我正在编写一个Windows Phone 7应用程序,用于从WeatherBug的API中提取天气数据。我遇到了7天预测数据的问题。我用XML获取数据,但我无法解析XML数据。
以下是调用Web服务并传递邮政编码的代码:
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&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&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="&deg;F">59</aws:high>
<aws:low units="&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="&deg;F">43</aws:high>
<aws:low units="&deg;F">35</aws:low>
</aws:forecast>
等等。
aws:forecast部分出现七次 - 预测中每天一次。它的每次迭代都有标题,短预测,图像,描述,预测,高和低元素。
我有一个名为Forecast.xaml的Windows Phone数据透视页面有七个项目。我的目标是每天都这样展示: “城市:威利斯顿公园” “日期:12/7/2011” “日:星期三” “描述:雨” “预测:下雨。在50年代的高点。北风5到10英里/小时,阵风高达20英里/小时。降雨量接近100%。” “高:59” “低:31”
没什么特别的。但我只是想办法从文件中提取单个元素并将它们分配给变量。
我已经阅读了Kotan Code的教程,但是我在将它应用到我的项目时遇到了麻烦。任何帮助将不胜感激。
答案 0 :(得分:4)
您可以使用Linq to XML - 只确保声明并使用正确的命名空间,在本例中是{XML}中定义的aws
:
..
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();