I'm trying to sort items before they're put into the combobox. Heres the code I'm using
public void InitializeDropDown(string XmlFile, string xpath)
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFile);
XPathNavigator navigator = doc.CreateNavigator();
XPathExpression expression = navigator.Compile(xpath);
expression.AddSort("name", XmlSortOrder.Descending,
XmlCaseOrder.UpperFirst,
string.Empty, XmlDataType.Text);
XPathNodeIterator iterator = navigator.Select(expression);
foreach (XPathNavigator item in iterator)
{
WeatherServicesCBO.Items.Add(item.Value);
}
}
I thought this would be the correct way to sort XML data, what am I missing?
EDIT: Here are some other techniques I've tried thus far either doesn't sort of I get an error.
First try (no sorting)
public void InitializeDropDown(string XmlFile, string xpath)
{
var doc = new XmlDocument();
doc.Load(XmlFile);
XPathNavigator navigator = doc.CreateNavigator();
XPathExpression expression = navigator.Compile(xpath);
expression.AddSort("@name", XmlSortOrder.Descending,
XmlCaseOrder.UpperFirst,
string.Empty, XmlDataType.Text);
XPathNodeIterator iterator = navigator.Select(expression);
foreach (XPathNavigator item in iterator)
{
WeatherServicesCBO.Items.Add(item.Value);
}
}
Second attempt (gives error The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments)
public void InitializeDropDown(string XmlFile, string xpath)
{
string[] services = { "Google Weather", "Yahoo! Weather", "NOAA", "WeatherBug" };
IEnumerable<string> query = from service in services
orderby service.Substring(0, 1) ascending
select service;
foreach (string @string in query)
WeatherServicesCBO.Items.Add(@string);
}
Third attempt (get a NullReferenceException)
public void InitializeDropDown(string XmlFile, string xpath)
{
var doc = XDocument.Load(XmlFile);
foreach (var item in doc.XPathSelectElements(xpath).OrderByDescending(n => n.Attribute("name").Value))
WeatherServicesCBO.Items.Add(item);
}