C# 需要帮助阅读C中的xml文件吗#
我正在为Windows phone制作一个程序,该程序将从xml文件中获取天气数据并将其显示在屏幕上。然而,我不断得到空值,我不确定我做错了什么 示例XML文件: 以下是我所拥有的:C# 需要帮助阅读C中的xml文件吗#,c#,windows-phone-7,silverlight-4.0,linq-to-xml,C#,Windows Phone 7,Silverlight 4.0,Linq To Xml,我正在为Windows phone制作一个程序,该程序将从xml文件中获取天气数据并将其显示在屏幕上。然而,我不断得到空值,我不确定我做错了什么 示例XML文件: 以下是我所拥有的: try { // get the stream containing the response from the async call streamResult = forecastState.AsyncResponse.GetResponseStream(); // load the X
try
{
// get the stream containing the response from the async call
streamResult = forecastState.AsyncResponse.GetResponseStream();
// load the XML
XElement xmlWeather = XElement.Load(streamResult);
// find the source element
XElement xmlCurrent = xmlWeather.Descendants("source").First();
// get city and height
xmlCurrent = xmlWeather.Descendants("location").First();
mTempCityName = (string)(xmlCurrent.Element("city"));
mTempHeight = (int)(xmlCurrent.Element("height"));
//Find the forecast time
xmlCurrent = xmlWeather.Descendants("time-layout").First();
//store time of day in array
mTimeOfDay = (string)(xmlCurrent.Attribute("period-name"));
//Find the temperature
xmlCurrent = xmlWeather.Descendants("temperature").First();
mTemp = (int)(xmlCurrent.Element("value"));
//now get the current weather conditions for each time period
xmlCurrent = xmlWeather.Descendants("weather").First();
mDescription = (string)(xmlCurrent.Attribute("weather-summary"));
//now get icon links for weather
xmlCurrent = xmlWeather.Descendants("conditions-icon").First();
mIcon = (string)(xmlCurrent.Attribute("icon-link"));
}
嗯:
没有名为时间布局
的属性(它有具有该属性的子元素)时段名称
- 我想温度应该没问题
没有weather
属性-它有具有该属性的子元素weather summary
没有条件图标
属性,它有图标链接
元素图标链接
请注意,对于要强制转换为
int
(顺便说一句,不需要额外的括号)的值,在这种情况下您应该得到一个异常-您显然不能得到实际的空值。是否有可能抛出异常而您没有注意到?你的捕猎区有什么?顺便说一句,没有“csharp”这样的东西。语言名为“C#”。你所有的值都是空值吗?实际上我可以得到城市名称和高度,但其余的都是空值。事实上,为什么不完全删除try/catch块?转换为int的值被分配给int变量,这样它们就不会引发异常。@SEAlink:但是如果找不到相关的属性/元素,它们应该引发异常,这就是其他地方发生的情况。@Jon Skeet:即使删除了try/catch块,调试器也不会引发异常。。。