【问题标题】:Parse XML Data in Windows Phone 7在 Windows Phone 7 中解析 XML 数据
【发布时间】:2011-12-08 02:50:57
【问题描述】:

我正在编写一个 Windows Phone 7 应用程序来从 WeatherBug 的 API 中提取天气数据。我遇到了 7 天预测数据的问题。我正在获取 XML 格式的数据,但我无法解析 XML 数据。

这是调用网络服务并传递邮政编码的代码:

public void GetForecastByZip(string zip)
    {         
        string url = ("http://" + apiCode + ".api.wxbug.net/getForecastRSS.aspx?ACode=" + apiCode + "&zipCode=" + zip);            

        WebClient xmlClient = new WebClient();
        xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
        xmlClient.DownloadStringAsync(new Uri(url));
    }

这是我获取 XML 并尝试用它做一些事情的代码。

void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            string xmlData = e.Result;
            XmlReader reader = XmlReader.Create(xmlData);

            XDocument doc = XDocument.Load(reader);

        }
    }

XML的格式如下:

<rss version="2.0" xmlns:georss="http://www.georss.org/georss">
  <channel>
<title>Forecast for Williston Park, NY - USA</title>
<link>http://weather.weatherbug.com/NY/Williston Park-weather/local-forecast/7-day-forecast.html?ZCode=Z5546&amp;Units=0</link>
<description>Weatherbug, the owner of the world's largest weather network is now providing an API to it's weather data in the
    form of RSS.  This will enable it's enthusiastic users to build their own applications.</description>
<language>en-us</language>
<lastBuildDate>Wed, 07 Dec 2011 20:56:00 GMT</lastBuildDate>
<ttl>60</ttl>
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0" />
<aws:WebURL>http://weather.weatherbug.com/NY/Williston Park-weather/local-forecast/7-day-forecast.html?ZCode=Z5546&amp;Units=0</aws:WebURL>
<aws:forecasts type="Detailed" date="12/7/2011 3:56:00 PM">
    <aws:location>
        <aws:city>Williston Park</aws:city>
        <aws:state>NY</aws:state>
        <aws:zip>11596</aws:zip>
        <aws:zone>NY179</aws:zone>
    </aws:location>
    <aws:forecast>
        <aws:title alttitle="WED">Wednesday</aws:title>
        <aws:short-prediction>Rain</aws:short-prediction>
        <aws:image isNight="1" icon="cond014.gif">http://deskwx.weatherbug.com/images/Forecast/icons/cond014.gif</aws:image>
        <aws:description>Wednesday</aws:description>
        <aws:prediction>Rain. Highs in the upper 50s. North winds 5 to 10 mph with gusts up to 20 mph. Chance of rain near 100 percent.</aws:prediction>
        <aws:high units="&amp;deg;F">59</aws:high>
        <aws:low units="&amp;def;F">31</aws:low>
    </aws:forecast>
    <aws:forecast>
        <aws:title alttitle="THU">Thursday</aws:title>
        <aws:short-prediction>Sunny</aws:short-prediction>
        <aws:image isNight="0" icon="cond007.gif">http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif</aws:image>
        <aws:description>Thursday</aws:description>
        <aws:prediction>Sunny. Highs in the lower 40s. West winds 15 to 20 mph... Diminishing to 5 to 10 mph in the afternoon.</aws:prediction>
        <aws:high units="&amp;deg;F">43</aws:high>
        <aws:low units="&amp;deg;F">35</aws:low>
    </aws:forecast>

等等等等

aws:forecast 部分出现七次 - 在预测中每天一次。它的每次迭代都有标题、短预测、图像、描述、预测、高和低元素。

我有一个名为 Forecast.xaml 的 Windows Phone 数据透视页面,其中包含七个项目。我的目标是每天都有这样的展示: “城市:威利斯顿公园” “日期:2011 年 12 月 7 日” “日期:星期三” 《描述:雨》 “预测:下雨。50 年代高点。北风 5 到 10 英里/小时,阵风高达 20 英里/小时。下雨的可能性接近 100%。” “高:59” “低:31”

没什么特别的。但是我只是想不出一种方法来从文件中提取单个元素并将它们分配给一个变量。

我已阅读 Kotan Code 上的教程,但无法将其应用到我的项目中。任何帮助将不胜感激。

【问题讨论】:

    标签: c# xml windows-phone-7


    【解决方案1】:

    您可以使用 Linq to XML - 只需确保您声明并使用正确的命名空间,在这种情况下 aws 在您的 XML 中定义:

    ..
    XDocument doc = XDocument.Load(reader);
    XNamespace aws = "http://www.aws.com/aws";
    
    var forecasts = doc.Descendants(aws + "forecast")
                       .Select(x => new
                        {
                           Title = x.Element(aws + "title").Value,
                           ShortPrediction = x.Element(aws + "short-prediction").Value,
                           Prediction = x.Element(aws + "prediction").Value,
                           //more here
                        })
                       .ToList();
    

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2012-11-15
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多