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

LVL 2
diaFAsked:
Who is Participating?

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

x
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
http://msdn.microsoft.com/en-us/library/b2hhe5h7(VS.80).aspx

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
http://msdn.microsoft.com/en-us/library/f7fta44c(VS.80).aspx
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.
eg:
// Create the output table.
DataTable yourDataTable = new DataTable();
yourDataTable.Columns.Add("ColID");
yourDataTable.Columns.Add("ColName);

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

DataView dv = new DataView(yourDataTable);
dv.sort = "ColName";
ddlRanges.dataValueField="ColID";
ddlRanges.dataTextField="ColName";
ddlRanges.datasource=dv;

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
amxCommented:
You could use LINQ to xml, here is an example from
http://mstanciu.blogspot.com/2008/08/sorting-elements-in-xml-document.html
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <node>3</node>
  <node>4</node>
  <node>1</node>
  <node>2</node>
  <node>5</node>
</root>
 
XDocument xml = XDocument.Load("XMLFile.xml");
xml.Root.ReplaceNodes(xml.Root.Elements("node").OrderBy(el => el.Value));
xml.Save("XMLFile.xml");

Open in new window

diaFAuthor Commented:

I am looking to sort the data in this xml source : http://www.toyota.co.za/api/Toyota.asmx/Dealers 
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
etc.
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();
        yourDataTable.Columns.Add("dealerCode");
        yourDataTable.Columns.Add("dealerName");
        yourDataTable.Columns.Add("region");
 
        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;
 
            yourDataTable.Rows.Add(myNewRow);
        }
 
        DataView dv = new DataView(yourDataTable);
        dv.Sort = "dealerName";
        ddlDealer.DataValueField = "dealerCode";
        ddlDealer.DataTextField = "dealerName";
        ddlDealer.DataSource = dv;
        ddlDealer.DataBind();

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
C#

From novice to tech pro — start learning today.