【问题标题】:How can I read this XML file in WP7?如何在 WP7 中读取此 XML 文件?
【发布时间】:2012-04-04 02:15:41
【问题描述】:

我正在尝试从一个 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;
        }
    }
}

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

【问题讨论】:

    标签: xml windows-phone-7


    【解决方案1】:

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

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

    在处理属性时,您可能可以使用“CityListPicker.Items.Add(city.Attribute("name")).Value;”之类的东西来摆脱困境

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

    希望这会有所帮助。

    【讨论】:

    • 感谢帮助,我正在使用system.xml.linq,我认为代码将所有数据都带入内存,是什么让应用程序卡住了!我认为有一些方法可以读取 xml 文件而不将其完全加载到内存中。谢谢
    • 如果问题在于将文件加载到内存中,也许这个链接会有所帮助:stackoverflow.com/questions/5838657/…
    【解决方案2】:

    尝试添加

    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 已加载...

    希望此解决方案对您有所帮助...

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多