Sort XML using the XPATHEXPRESSION class
NET_LOVER
If you have used XSLT, when you need to sort the strings or numbers in the XML document, you may be accustomed to using the XSL: Sort element, looping in XSL: for-Each or using XSL: Apply-Templates When calling a template, this element is convenient and simple. However, in some cases, you just want to simply sort and display data for XML documents, then XSL: sort will greatly reduce computer performance, explain it, how to do not use XSL: Sort elements , To be sorted to XML data. Under the .NET platform, it is very easy to complete such a job, we only need to use several classes in the SYSTEM.XML.XPath name space, such as Xpathnavigator and XPATHEXPIPRESSION. These classes are equivalent to the functionality of XPath in the XSLT, allowing you to be traversed in the XML document, or can be sorted. Below is a simple description of the class in the SYSTEM.XML.XPath name space: XPathDocument: When performing an XML document, provide a fast, efficient, read-only cache feature, is recommended to use this class when performing XSLT conversion . XPATHEXCEPTION: When processing XPath generates an error, an exception is thrown. XPATHEXPRESSION: The compiled XPath expression when the compile () method is called when calling the XPathnavigator. Xpathnavigator: Provides a pointer model for our read of any type of data that supports the IXPathnavigable interface. XpathNodeITeiterator: Traversing Node Collection. Because of the details of each class, we will only discuss the use of those classes associated with sorting. First, we need to create an XPathnavigator object to establish XPath expressions to complete our sorting function. Since this class is an abstract class, so we can not directly create it: XPathNavigator nav = new XPathNavigator (); we have to use CreateNavigator XmlDocument, XmlDataDocument or the XPathDocument () method to create: XPathDocument doc = new XPathDocument (Server.MapPath ( " Customers.xml "); XPathnavigator NAV = doc.createnavigator (); After you create an XPathnavigator object, we can use the Compile () method to compile an XPath expression, this method returns an XPATHEXPRESSION class, which encapsulates the expression Allow us to sort.