• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

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

0
diaF
Asked:
diaF
1 Solution
 
Anurag ThakurCommented:
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
0
 
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;

0
 
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

0
 
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 :(.
0
 
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

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now