如何在WP7中读取此XML文件?

时间:2012-04-04 02:15:41

标签: xml windows-phone-7

我正在尝试从银色光源的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;
        }
    }
}

城市在很长一段时间后加载,或者没有加载! 我的代码有问题吗?

2 个答案:

答案 0 :(得分:0)

好吧,我可能完全偏离了这里,因为我对windows-phone-7一无所知,但我假设你的LoadButton_Click方法的核心是Linq到Xml。

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

在处理属性时,您可以使用类似“CityListPicker.Items.Add(city.Attribute(”name“))的东西。值;”

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

希望这会有所帮助。

答案 1 :(得分: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 ...

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

相关问题