Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要帮助阅读C中的xml文件吗#_C#_Windows Phone 7_Silverlight 4.0_Linq To Xml - Fatal编程技术网

C# 需要帮助阅读C中的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

我正在为Windows phone制作一个程序,该程序将从xml文件中获取天气数据并将其显示在屏幕上。然而,我不断得到空值,我不确定我做错了什么

示例XML文件:

以下是我所拥有的:

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
    属性-它有具有该属性的子元素
  • 条件图标
    没有
    图标链接
    属性,它有
    图标链接
    元素
换句话说,对于每一位,您都需要准确地查看XML包含什么,然后准确地查看您所要求的内容,小心地区分元素和属性


请注意,对于要强制转换为
int
(顺便说一句,不需要额外的括号)的值,在这种情况下您应该得到一个异常-您显然不能得到实际的空值。是否有可能抛出异常而您没有注意到?你的捕猎区有什么?

顺便说一句,没有“csharp”这样的东西。语言名为“C#”。你所有的值都是空值吗?实际上我可以得到城市名称和高度,但其余的都是空值。事实上,为什么不完全删除try/catch块?转换为int的值被分配给int变量,这样它们就不会引发异常。@SEAlink:但是如果找不到相关的属性/元素,它们应该引发异常,这就是其他地方发生的情况。@Jon Skeet:即使删除了try/catch块,调试器也不会引发异常。。。