Sort XML files using the xpathexpression class

Source: Internet
Author: User


Xpathexpression can be used to sort the content of an XML file by an attribute column. Suppose we have an XML file with the following content:

<Cities>
<City name = "Beijing" number = "001"/>
<City name = "Shanghai" number = "002">
<City name = "Dalian" number = "004">
<City name = "Shenzhen" number = "003">
</Cities>

How can I sort nodes in the XML file by name? See the followingCode:

 Private   String Sortxmlfile ( String Xmlstring)
{
Stringbuilder sb = Null ;
Using (Stringreader ST =New Stringreader (xmlstring ))
{
Xpathdocument xpdoc = New Xpathdocument (ST );
Xpathnavigator xpnav = xpdoc. createnavigator ();

SB = New Stringbuilder ( @" <Cities> " );
Xpathexpression xpexp = xpnav. Compile ( @" Cities/City " );
Xpexp. addsort ( " @ Name " , Xmlsortorder. ascending, xmlcaseorder. None, "" , Xmldatatype. Text );
Xpathnodeiterator xpnodeiter = xpnav. Select (xpexp );
While (Xpnodeiter. movenext ())
{
SB. appendline (xpnodeiter. Current. outerxml );
}
SB. appendline ( @" </Cities> " );
}
Return SB. tostring ();
}

The returned string is the content of the sorted XML file.

Next, I will explain it in detail.ProgramMeanings of these classes used ~~ (Class in system. xml. XPath namespace)

xpathdocument: provides a fast, effective, and read-only cache function for XML document processing. This class is recommended for XSLT conversion.
xpathexception: An exception is thrown when an error occurs during XPath processing.
xpathexpression: The compiled XPath expression generated when xpathnavigator's compile () method is called.
xpathnavigator: provides a pointer model for us to read any type of data that supports the ixpathnavigable interface.
xpathnodeiterator: traverses the node set.
because the details of each class are beyond the scope of our topic, we only discuss the use of those classes related to sorting. First, we need to create an xpathnavigator object to create an XPATH expression to complete our sorting function. Because this class is an abstract class, we cannot directly create it like this:
xpathnavigator nav = new xpathnavigator ();
you must use the createnavigator () method of xmldocument, xmldatadocument, or xpathdocument to create a file:
xpathdocument Doc = new xpathdocument (server. mappath ("cities. XML ");
xpathnavigator nav = Doc. createnavigator ();
after the xpathnavigator object is created, we can compile an XPATH expression using the compile () method. This method returns an xpathexpression class, it encapsulates compiled expressions and allows us to sort them. The following is an example of creating a compiled XPath expression using the xpathnavigator class:
xpathexpression exp = nav. Compile (XPath);

We use the addsort () method of the xpathexpression object to implement the sorting function. This method has two overload methods:
 
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 sorting expression. The second method has five parameters: the object to be sorted, the sorting order, the conditional order, the language category, and the data type, the following is an example of sorting using this method:
 
Exp. addsort ("text ()", xmlsortorder. ascending, xmlcaseorder. None, "", xmldatatype. Text );
 
Once we add the sort function to the XPath expression, we can call the select () method of the xpathnavigator object and take the compiled XPath expression as the parameter, select () method returns an xpathnodeiterator object, which can be used to traverse the selected node.

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.