开发者

Sorting data from XML file with C# : Not Sorting

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);

}


your code seems correct and must work fine. you might have missed some things:

1 . XML is a case sensitive language, so don't use "name" instead of "Name".

2 . the first parameter of AddSort method must be a relative xpath. so if you mean the attribute named "name" in the node you are referring, use "@name".

consider this XML :

<a>
  <b foo="bb">
    <name>KKKK</name>
  </b>
  <b foo="aa">
    <name>AAAA</name>
  </b>
</a>

you can write:

expression.AddSort("name" ,.. 

or

expression.AddSort("@foo", ...

Anyway if I were you, I would use Linq to xml:

public void InitializeDropDown2(string XmlFile, string xpath)
        {
            var document = XDocument.Load(XmlFile);
            foreach (var item in document.XPathSelectElements(xpath).OrderByDescending(n=>n.Attribute("foo").Value))
            {
                comboBox1.Items.Add(item.Value);
            }
        }


You should consider using Linq to XML and the OrderBy standard query operator.


This method worked perfectly (once I added __using System.Collections.Generic;.Once I did that all was fine

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);
}

Now all I need to do is get the values from the XML document into a string array. Thanks everyone for your help (I you know of a way to get the data from XML file into string array share ;))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜