0

我正在尝试从一个 XML 文件中加载国家名称及其城市。

我希望根据所选国家/地区将城市加载到 ListPicker。

这是我的 XML 文件的一部分:

<Times>
    <country name="USA">
        <city name="Aaronsburg -- PA">
            <state>PA</state>
            <latitude>408739</latitude>
            <longitude>-773815</longitude>
            <timezone>-500</timezone>
            <daylight>1</daylight>
        </city>
        <city name="Abbeville -- AL">
            <state>AL</state>
            <latitude>316077</latitude>
            <longitude>-853051</longitude>
            <timezone>-600</timezone>
            <daylight>1</daylight>
        </city>
        <city name="Abbeville -- GA">
            <state>GA</state>
            <latitude>319710</latitude>
            <longitude>-833016</longitude>
            <timezone>-500</timezone>
            <daylight>1</daylight>
        </city>
        <city name="Abbot -- ME">
            <state>ME</state>
            <latitude>453219</latitude>
            <longitude>-695342</longitude>
            <timezone>-500</timezone>
            <daylight>1</daylight>
            </city>
........
........

这是我写的代码:

private void LoadButton_Click(object sender, RoutedEventArgs e)
{
    XDocument doc = XDocument.Load("Athan.xml");
    var definitions = doc.Document.Descendants(XName.Get("country"));

    foreach (var definition in definitions)
    {
        if (definition.Attribute(XName.Get("name")).Value == CountryListPicker.SelectedItem.ToString())
        {
            var cities = definition.Document.Descendants(XName.Get("city"));

            foreach (var city in cities)
            {
                CityListPicker.Items.Add(city.Attribute(XName.Get("name")).Value.ToString());
            }

            return;
        }
    }
}

城市加载了半天,还是没有加载!我的代码有问题吗?

4

2 回答 2

0

好的,我可能在这里完全偏离基础,因为我对 windows-phone-7 一无所知,但我假设您的 LoadButton_Click 方法的核心是 Linq to Xml。

如果是这种情况,我想知道您对 .Document 的调用以及对 XName 的频繁调用。例如,在我看来,您的定义代码可能很简单,例如“var definitions = doc.Descendants("country");" 对于“var city”行也是如此。

在处理属性时,您可能能够摆脱诸如“CityListPicker.Items.Add(city.Attribute("name")).Value;”之类的东西。

因为数据至少在某些时候加载,我假设您没有命名空间问题,但您应该验证。

希望这可以帮助。

于 2012-04-04T03:07:21.930 回答
0

尝试添加

XDocument doc = XDocument.Load("Athan.xml");
XElement root = doc.Root;

foreach (XElement el in root.Descendants("Times"))
{
    // code to navigate in your XML

    if ( el.Name == "country"){
        foreach ( XAttribute attr in el.Attributes())
        {
            if ( attr.name == "name"){
                // receive your country name
                String CountryName = attr.Value;
            }
        }
        foreach (XElement city in el.Descendants())
        {
            if (city.Name == "city")
            {
                Object City = new Object(); // create your object...
                foreach ( XAttribute attr in el.Attributes())
                {
                    if ( attr.name == "state"){
                        City.sate = cityAttr.Value;
                    }
                    if ( attr.name == "latitude"){
                        City.latitude = cityAttr.Value;
                    }
                    // etc....
                }

          //You add your object city in list ( for example ) ...
    }
}

我在加载我的 xml 时遇到问题,使用这种方法,xml 被加载...

希望这个解决方案可以帮助你...

于 2012-04-05T09:59:11.930 回答