Link to home
Start Free TrialLog in
Avatar of diaF
diaFFlag for South Africa

asked on

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

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

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
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of diaF

ASKER


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 :(.
Avatar of diaF

ASKER

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