【发布时间】:2014-01-20 15:12:29
【问题描述】:
我尝试将此 XML 天气信息解析到我的应用程序中。
<weather>
<date>2014-01-03
</date>
<chanceofsnow>0</chanceofsnow>
<totalSnowfall_cm>0.0</totalSnowfall_cm>
<top>
<maxtempC>-3</maxtempC>
<maxtempF>27</maxtempF>
<mintempC>-5</mintempC>
<mintempF>24</mintempF>
</top>
<hourly>
<time>100</time>
<top>
<tempC>-6</tempC>
<tempF>20</tempF>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirDegree>213</winddirDegree>
<winddir16Point>SSW</winddir16Point>
<weatherCode>113</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png]]> </weatherIconUrl>
<weatherDesc><![CDATA[Clear]]></weatherDesc>
</top>
</hourly>
</weather>
我使用下面的 C# 来解析它:
XElement XmlSneeuw = XElement.Parse(e.Result);
//current
listBoxVandaag.ItemsSource
= from weather in XmlSneeuw.Descendants("weather")
select new AlgemeneInformatie
{
Chance_of_Snow = weather.Element("chanceofsnow").Value,
Total_Snowfall = weather.Element("totalSnowfall_cm").Value,
};
//Current
listBoxVandaagTop.ItemsSource
= from weather1 in XmlSneeuw.Descendants("top")
select new AlgemeneInformatieTop
{
Actueel_Top_maxtempC = weather1.Element("maxtempC").Value,
Actueel_Top_mintempC = weather1.Element("mintempC").Value,
};
但是现在我的 xml 中有 2 个 TOP 元素,所以这不起作用。做这个的最好方式是什么?这是解析此类信息的正确方法吗?
我使用这个网站作为参考: http://developer.nokia.com/Community/Wiki/Weather_Online_app_for_Windows_Phone
【问题讨论】:
-
按照 Xenolightning 的建议,您可以使用 XPath 并查看此参考以了解更多信息:codeproject.com/Articles/9494/…
标签: c# xml xml-parsing