Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
 <Employee EmpId="1'" Name="Sam" />
 <Employee EmpId="2" Name="Lucy"/>
 <Employee EmpId="3" Name="Kate"/>
 <Employee EmpId="4" Name="Chris"/>
 <Employee EmpId="5" Name="David"/>
 <Employee EmpId="6" Name="Zolar"/>
 <Employee EmpId="7" Name="Shannon"/>
</Employees>


Hello All ,
Based on Name Attribute selected i have to display the xml in sorted order how shall i do this?

Thanks In Advance
Posted
Comments
Sandeep Mewara 17-May-12 1:20am    
Why? can you share some story around why are you doing it and when are you going to use it?
yeshgowda 18-May-12 0:06am    
It was just an assignment :) i searched in google how to sort based on attribute.I never got an answer. hence i asked the question

1 solution

Hi,

You may try XPathNavigator and XPathExpression


using System;
using System.Xml;
using System.Xml.XPath;


namespace TestApp_Console_3
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Test.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression exp = nav.Compile("Employees/Employee");
exp.AddSort("@Name", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);

foreach (XPathNavigator t in nav.Select(exp))
{
Console.WriteLine(t.OuterXml);
}
Console.ReadLine();
}
}
}
 
Share this answer
 
Comments
yeshgowda 18-May-12 0:04am    
Thanks for ur answer :)
When i use Name and EmpId id in combobox based on selected item how to add object expr in exp.AddSort(" ".......);
Sunny_Kumar_ 18-May-12 0:11am    
just compile the XPathExpresion Object using XPathNavigator ("nav.compile(...) in above example") on selection change and sort them.
Cheers !!
yeshgowda 19-May-12 1:56am    
Thanks for ur sujjestion
Sunny_Kumar_ 19-May-12 2:15am    
your most welcome!! Happy Coding :)

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