I have a dropdownlist that get its list from XMLDataSource, the list is for list of Airports and I want to show them in DropDownList sorted either Desc or Asc, How can I accomplish that.
<?xml version="1.0" encoding="utf-8" ?>
<Airports>
<airport code ="-1" city ="" Airport ="-Airport-" country =""></airport>
<airport code ="AAA" city ="Anaa" Airport ="Anaa" country ="PF"></airport>
<airport code ="AAB" city ="Arrabury" Airport ="Arrabury" country ="AU"></airport>
<airport code ="AAC" city ="Arish" Airport ="El Arish International Airport" country ="EG"></airport>
<airport code ="AAD" city ="Ad-Dabbah" Airport ="Ad-Dabbah" country ="SD"></airport>
<airport code ="AAE" city ="Annaba" Airport ="Les Salines" country ="DZ"></airport>
<airport code ="AAF" city ="Apalachicola, FL" Airport ="Municipal" country ="US"></airport>
<airport code ="AAG" city ="Arapoti" Airport ="Arapoti" country ="BR"></airport>
<airport code ="AAH" city ="Aachen" Airport ="Aachen/Merzbrück" country ="DE"></airport>
<airport code ="AAI" city ="Arraias" Airport ="Arraias" country ="BR"></airport>
<airport code ="AAJ" city ="Awaradam" Airport ="Cayana Airstrip" country ="SR"></airport>
<airport code ="AAK" city ="Aranuka" Airport ="Aranuka" country ="KI"></airport>
<airport code ="AAL" city ="Aalborg" Airport ="Aalborg" country ="DK"></airport>
<airport code ="AAM" city ="Mala Mala" Airport ="Mala Mala" country ="ZA"></airport>
<airport code ="AAN" city ="Al Ain" Airport ="Al Ain International Airport" country ="AE"></airport>
<airport code ="AAO" city ="Anaco" Airport ="Anzualegui" country ="VE"></airport>
<airport code ="AAP" city ="Houston, TX" Airport ="Andrau Airpark (closed 1998)" country ="US"></airport>
.....
</Airports>
“First, solve the problem. Then, write the code.”
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)
Nov 29, 2010 12:43 AM|nvanhaaster@resultstel.com|LINK
Hello,
From my understanding that without going to the code behind you will not be able to sort the XML files, however there are many ways to sort it from the code behind. You can use XML Transform schema, load into a custom object or use a generic list<t>
I prefer the Custom Objects, and even using some generic sorting methods to handle the sorting for which ever header and whichever direction you wish. There are some great samples out there if you google Sorting generics.
However for fun I wrote up pretty much everything you would need to create custom objects, with sortable types (ascending, descending) and also against any of the properties you wish.
All that is asking for is the path to the XML, how you want to sort the list, and in which direction you would like to sort on (asc, desc)
Additionally I have included some classes for your airports, including parseing the sample XML provided.
Included Files
Airport.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Airport
/// </summary>
///
[Serializable]
public class Airport
{
private string code;
public string Code
{
get { return code; }
set { code = value; }
}
private string city;
public string City
{
get { return city; }
set { city = value; }
}
private string airport;
public string AirportName
{
get { return airport; }
set { airport = value; }
}
private string country;
public string Country
{
get { return country; }
set { country = value; }
}
}
Airports.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Xml.Linq;
using System.ComponentModel;
/// <summary>
/// Summary description for Airports
/// </summary>
public class Airports : IEnumerable
{
public enum AirportSortingDirection { Ascending, Descending }
public enum AirportSortingBy { AirportName, City, Country, Code }
public Airports(string XMLFilePath, AirportSortingBy SortBy, SortDirection Direction )
{
airList = new List<Airport>();
XDocument doc = XDocument.Load(XMLFilePath);
var query = from rows in doc.Descendants("Airports").Elements("airport")
select rows;
foreach (var q in query)
{
Airport entity = new Airport();
entity.AirportName = q.Attribute("Airport").Value;
entity.City = q.Attribute("city").Value;
entity.Code = q.Attribute("code").Value;
entity.Country = q.Attribute("country").Value;
airList.Add(entity);
}
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Airport));
PropertyDescriptor propertyDesc = properties.Find(SortBy.ToString(), true);
PropertyComparer<Airport> pc = new PropertyComparer<Airport>(propertyDesc.Name, Direction);
airList.Sort(pc);
}
public List<Airport> airList;
public IEnumerator GetEnumerator()
{
return new AirportEnumerator(this);
}
private class AirportEnumerator : IEnumerator
{
private int position = -1;
private Airports _airports;
public AirportEnumerator(Airports airports)
{
this._airports = airports;
}
public bool MoveNext()
{
if (position < _airports.airList.Count-1)
{
position++;
return true;
}
else
return false;
}
public object Current
{
get { if (_airports.airList.Count == 0)
return null;
else
return _airports.airList[position]; }
}
public void Reset() { position = -1; }
}
}
Sort Direction.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for SortBy
/// </summary>
public enum SortDirection
{
Ascending,
Descending
}
well thanks Nico to your greate contribution but its really long one for such a small task. I need a simpler method
“First, solve the problem. Then, write the code.”
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)
Thanks Decker LINQ seamed the proper solution for my case. though I modified on what u suggest so the sort happens also by LINQ
here is the final code
protected void ddlDepartureCountry_SelectedIndexChanged(object sender, EventArgs e)
{
var query = from co in XDocument.Load(Server.MapPath("XML Documents/Airports.xml")).Descendants("airport")
where co.Attribute("country").Value == ddlDepartureCountry.Text
orderby co.Attribute("Airport").Value ascending
select new
{
airport = (String)co.Attribute("Airport"),
code = (String)co.Attribute("code"),
country = (String)co.Attribute("country")
};
ddlDepartureAirport.DataSource = query;
ddlDepartureAirport.DataBind();
}
dropdownlistlinqsort
“First, solve the problem. Then, write the code.”
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)
Member
345 Points
150 Posts
Sort results from XMLDataSource
Nov 28, 2010 11:09 PM|jaffal|LINK
Hi,
I have a dropdownlist that get its list from XMLDataSource, the list is for list of Airports and I want to show them in DropDownList sorted either Desc or Asc, How can I accomplish that.
Thanks in Advance
XmlDataSource XML
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)
Contributor
6434 Points
1435 Posts
Re: Sort results from XMLDataSource
Nov 29, 2010 12:43 AM|nvanhaaster@resultstel.com|LINK
Hello,
From my understanding that without going to the code behind you will not be able to sort the XML files, however there are many ways to sort it from the code behind. You can use XML Transform schema, load into a custom object or use a generic list<t>
I prefer the Custom Objects, and even using some generic sorting methods to handle the sorting for which ever header and whichever direction you wish. There are some great samples out there if you google Sorting generics.
However for fun I wrote up pretty much everything you would need to create custom objects, with sortable types (ascending, descending) and also against any of the properties you wish.
You can download a sample here
Very easy to use here is a sample code behind
All that is asking for is the path to the XML, how you want to sort the list, and in which direction you would like to sort on (asc, desc)
Additionally I have included some classes for your airports, including parseing the sample XML provided.
Included Files
Airport.cs
Airports.cs
Sort Direction.cs
<div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Collections.Generic;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Linq;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Web;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Collections;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.Xml.Linq;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">using System.ComponentModel;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">/// <summary></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">/// Summary description for Airports</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">/// </summary></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">public class Airports : IEnumerable</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">{</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public enum AirportSortingDirection { Ascending, Descending }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public enum AirportSortingBy { AirportName, City, Country, Code }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public Airports(string XMLFilePath, AirportSortingBy SortBy, SortDirection Direction )</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> airList = new List<Airport>();</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> XDocument doc = XDocument.Load(XMLFilePath);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> var query = from rows in doc.Descendants("Airports").Elements("airport")</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> select rows;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> foreach (var q in query)</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Airport entity = new Airport();</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> entity.AirportName = q.Attribute("Airport").Value;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> entity.City = q.Attribute("city").Value;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> entity.Code = q.Attribute("code").Value;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> entity.Country = q.Attribute("country").Value;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> airList.Add(entity);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Airport));</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> PropertyDescriptor propertyDesc = properties.Find(SortBy.ToString(), true);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> PropertyComparer<Airport> pc = new PropertyComparer<Airport>(propertyDesc.Name, Direction);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> airList.Sort(pc);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public List<Airport> airList;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public IEnumerator GetEnumerator()</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return new AirportEnumerator(this);</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> private class AirportEnumerator : IEnumerator</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> private int position = -1;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> private Airports _airports;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public AirportEnumerator(Airports airports)</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> this._airports = airports;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public bool MoveNext()</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> if (position < _airports.airList.Count-1)</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> position++;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return true;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> else</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return false;</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public object Current</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> get { if (_airports.airList.Count == 0) </div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return null; </div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> else </div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> return _airports.airList[position]; }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> public void Reset() { position = -1; }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div> <div style="position: absolute; left: -10000px; top: 896px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">}</div>PropertyComparer.cs (generic class everyone should have)
Seems like alot of work but this is a fully contained managed class for your airports list.
Good luck,
Nico
My .Net Blog
My Links are shrunk by t-ny.co
All-Star
94120 Points
18111 Posts
Re: Sort results from XMLDataSource
Nov 29, 2010 10:01 PM|Decker Dong - MSFT|LINK
Hey, you can use LINQ to read a specific list of strings together and use List.Sort method. Something like this:
List<string> strings = from e in XDocument.Load("xxx.xml").Descades("airport")
select e.Attribute("code").Value;
strings.Sort();
then binding to your ddrList...
Member
345 Points
150 Posts
Re: Sort results from XMLDataSource
Nov 29, 2010 10:10 PM|jaffal|LINK
well thanks Nico to your greate contribution but its really long one for such a small task. I need a simpler method
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)
Member
345 Points
150 Posts
Re: Sort results from XMLDataSource
Nov 29, 2010 11:42 PM|jaffal|LINK
Thanks Decker LINQ seamed the proper solution for my case. though I modified on what u suggest so the sort happens also by LINQ
here is the final code
dropdownlist linq sort
(John Johnson)
________________________________________________
Please mark as Answer if it helped you :)