Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
How to sort an XMl file node base on its ID using C#.

I know how to edit attributes of an element in XML file.

But how to sort the whole XML file by its ID.

Is there any way to do so.

If so then can anyone get me a code snippet to solve this problem
Posted
Updated 17-Mar-14 23:58pm
v2

XmlDocument myXmlDoc = new XmlDocument();

myXMLNode = myXmlDoc.SelectSingleNode("record/Page1");
page1 is the node,
record is the root,

you enter your own root and node to sort

string Cubs_Account_Number = myXMLNode["CUBS_Account_Number"].InnerText;

you can sort like that

Sample xml file::

C#
<record>
<page1>

<cubs_account_number>123456789</cubs_account_number>
</page1>

</record>
 
Share this answer
 
v2
Comments
KUMAR619 18-Mar-14 8:35am    
How can we sort
Accessing the values then how to sort
nandakishoreroyal 18-Mar-14 8:36am    
how should you sort, based on value or node??
KUMAR619 18-Mar-14 8:44am    
I want to sort by node i.e each node has an attribute say Id="5"

I want to sort based on that node Id
KUMAR619 18-Mar-14 9:53am    
Please reply
XML
<Root a="1">
   <I aa="1" b="2">
   <I aa=5" b="2">
   <I aa="3" b="2">
   <I aa="4" b="2">
</Root>


var xml= xDoc.Element(&quot;Root&quot;)
                .Elements(&quot;I&quot;)
                .OrderByDescending(s =&gt; (int) s.Attribute(&quot;aa&quot;));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900