To sort XML by using the XPathExpression class

Source: Internet
Author: User
Tags abstract object expression string sort tostring xpath xsl
express|xml| sort If you've ever used XSLT, when you need to sort strings or numbers in an XML document, you might be used to using xsl:sort elements, looping through Xsl:for-each, or using xsl: The function of this element is convenient and simple when Apply-templates calls the template. However, in some cases, you simply want to simply sort and display the XML document, then Xsl:sort will greatly degrade your computer's performance, and here's how to sort the XML data without using the Xsl:sort element.
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:

XPathDocument: Provides a fast, efficient, read-only cache feature when processing XML documents, and is recommended for use in XSLT transformations.
XPathException: When processing XPath produces an error, an exception is thrown.
XPathExpression: A compiled XPath expression that is generated when the compile () method that invokes XPathNavigator is present.
XPathNavigator: Provides a pointer model that allows us to read any type of data that is saved for the IXPathNavigable interface.
XPathNodeIterator: Traverses the node collection.

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,
Xmlcaseorder 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 ();
}

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.