Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting data from XML file with C# : Not Sorting
    primarykey
    data
    text
    <p>I'm trying to sort items before they're put into the combobox. Heres the code I'm using</p> <pre><code>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); } } </code></pre> <p>I thought this would be the correct way to sort XML data, what am I missing?</p> <p><strong>EDIT</strong>: Here are some other techniques I've tried thus far either doesn't sort of I get an error. </p> <p>First try (no sorting)</p> <pre><code>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); } } </code></pre> <p>Second attempt (gives error <strong>The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments</strong>)</p> <pre><code>public void InitializeDropDown(string XmlFile, string xpath) { string[] services = { "Google Weather", "Yahoo! Weather", "NOAA", "WeatherBug" }; IEnumerable&lt;string&gt; query = from service in services orderby service.Substring(0, 1) ascending select service; foreach (string @string in query) WeatherServicesCBO.Items.Add(@string); } </code></pre> <p>Third attempt (get a NullReferenceException)</p> <pre><code>public void InitializeDropDown(string XmlFile, string xpath) { var doc = XDocument.Load(XmlFile); foreach (var item in doc.XPathSelectElements(xpath).OrderByDescending(n =&gt; n.Attribute("name").Value)) WeatherServicesCBO.Items.Add(item); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload