XPathExpression class to sort XML

Source: Internet
Author: User
Tags abstract tostring xpath xslt

XPathExpression class to sort XML

Under the. NET platform, it's easy to do this, and we just need to use several classes in the System.Xml.XPath namespace, such as XPathNavigator and XPathExpression. These classes correspond to the functions of XPath in XSLT, allowing you to iterate through an XML document, or you can sort the operation. The following is a simple description of the classes in the System.Xml.XPath namespace:

Since the details of each class are discussed in detail, beyond the scope of our topic, we only discuss the use of those classes related to the sort. First, we need to create a XPathNavigator object to build an XPath expression to complete our sorting function. Since this class is an abstract class, we cannot create it directly:

XPathNavigator nav = new XPathNavigator ();

We must use the XmlDocument, XmlDataDocument, or XPathDocument CreateNavigator () method to create:


XPathDocument doc = new XPathDocument (Server.MapPath ("customers.xml"));
XPathNavigator nav = Doc.createnavigator ();

After creating the XPathNavigator object, we can compile an XPath expression using the compile () method, which returns a XPathExpression class that encapsulates the compiled expression while allowing us to sort. Here is an example of using the XPathNavigator class to create a compiled XPath expression:

xpathexpression exp = nav.compile (XPath);

We use the XPathExpression object's AddSort () method to implement the sort function, which has two overloaded ways:

Public
abstract void AddSort (object expr, IComparer comparer);

public abstract void AddSort (
Object expr,
Xmlsortorder Order,
Xmlcaseo Tutorial Rder Caseorder,
String Lang,
Xmldatatype datatype
);

The first method allows us to use a custom sort expression; The second method has 5 parameters: the object to sort, the sort order, the conditional order, the language category, and the data type, and the following is an example of sorting using this method:

Exp.addsort ("text ()", Xmlsortorder.ascending,xmlcaseorder.none, "", Xmldatatype.text);

Once we have added a sort function to the XPath expression, we can invoke the XPathNavigator object's Select () method and take the compiled XPath expression as an argument, and the Select () method returns a XPathNodeIterator object. We can use it to traverse the nodes we have chosen.

Here are all the code that implements the sort feature written in C #:


private void Page_Load (object sender, System.EventArgs e) {
StringBuilder unsorted = new StringBuilder ();
StringBuilder sorted = new StringBuilder ();
String XPath = "/customers/customer/contactname";

XPathDocument doc = new XPathDocument (Server.MapPath ("customers.xml"));
XPathNavigator nav = Doc.createnavigator ();

XPathNodeIterator nodeiter1 = Nav.select (XPath);
while (Nodeiter1.movenext ()) {
Unsorted.append (nodeiter1.current.value + "<br/>");
}
This.lblunsorted.text = Unsorted.tostring ();

XPathExpression exp = nav.compile (XPath);
Exp.addsort ("text ()", Xmlsortorder.ascending,
Xmlcaseorder.none, "", Xmldatatype.text);

XPathNodeIterator nodeiter2 = Nav.select (exp);
while (Nodeiter2.movenext ()) {
Sorted.append (nodeiter2.current.value + "<br/>");
}
This.lblsorted.text = Sorted.tostring ();
}


XPathDocument: Provides a fast, efficient, read-only cache feature when processing XML documents, and is recommended for use in XSLT transformations.

XPathException: An exception is thrown when processing XPath produces an error.

XPathExpression: A compiled XPath expression that is generated when the compile () method that calls XPathNavigator is present.

XPathNavigator: Provides a pointer model for us to read any type of data that is saved to support the IXPathNavigable interface.

XPathNodeIterator: Traverses the node collection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.