Sort items from xml source before populate dropdown

How do I sort items from my xml source before populating the dropdown?
XmlNode xnode = ts.Ranges();
ddlRanges.Items.Add(new ListItem("--Select One --", "0"));
foreach (XmlNode n in xnode)
   ddlRanges.Items.Add(new ListItem(n.FirstChild.InnerText, n.FirstChild.NextSibling.InnerText));

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
XPath does not permit you to do sorting the data as you go through the XML
but you can use XPathExpression.AddSort to help you sort

or else you can first loop around the xml and add all the elements to a sorteddictionary object and then do the databinding of the dropdown with the sorted dictionary
David H.H.LeeCommented:
Hi diaF,
Perhaps you can add each record to datarow during iteration process and add it into dynamic created datatable.
After that, you can sort the datatable via DataRowFilter() method.
// Create the output table.
DataTable yourDataTable = new DataTable();

foreach (XmlNode n in xnode)
   DataRow myNewRow;
  myNewRow = yourDataTable.NewRow();
   myNewRow["ColID"] = n.FirstChild.NextSibling.InnerText;  
   myNewRow["ColName"] = n.FirstChild.InnerText ;  

DataView dv = new DataView(yourDataTable);
dv.sort = "ColName";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You could use LINQ to xml, here is an example from
<?xml version="1.0" encoding="utf-8" ?>
XDocument xml = XDocument.Load("XMLFile.xml");
xml.Root.ReplaceNodes(xml.Root.Elements("node").OrderBy(el => el.Value));

Open in new window

diaFAuthor Commented:

I am looking to sort the data in this xml source : 
Where i would have a dropdown with the values like this for example :
Eastern Cape - Dealership Name1
Eastern Cape - Dealership Name2
Eastern Cape - Dealership Name3
Eastern Cape - Dealership Name3
Kwa Zulu Natal - Dealership Name1
Thanks for your assistance. Btw, The resource is currently unavailable :(.
diaFAuthor Commented:
My solution is attached, thanks for your help guys :)
 XmlNode dealersregions = ts.Dealers();
        // Create the output table.
        DataTable yourDataTable = new DataTable();
        foreach (XmlNode n in dealersregions)
            DataRow myNewRow;
            myNewRow = yourDataTable.NewRow();
            myNewRow["dealerCode"] = n.FirstChild.NextSibling.InnerText;
            myNewRow["dealerName"] = n.FirstChild.NextSibling.NextSibling.NextSibling.InnerText + " - " + n.FirstChild.InnerText;
        DataView dv = new DataView(yourDataTable);
        dv.Sort = "dealerName";
        ddlDealer.DataValueField = "dealerCode";
        ddlDealer.DataTextField = "dealerName";
        ddlDealer.DataSource = dv;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.