C# 从节点获取值

C# 从节点获取值,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我有一个问题)如何从特定节点(信息/天气/日/日)获取值 这段代码返回两个值(+5,+6),但我只需要一个(+5)并抛出异常。尝试类似的方法 if (elem != null) elem.Descendants("day_part").Where(el => el.Attribute("type").Value == "день") .ToList().ForEach(el => listBox1.Items.Add(el.E

我有一个问题)如何从特定节点(信息/天气/日/日)获取值


这段代码返回两个值(+5,+6),但我只需要一个(+5)并抛出异常。

尝试类似的方法

 if (elem != null)
            elem.Descendants("day_part").Where(el => el.Attribute("type").Value == "день")
                .ToList().ForEach(el => listBox1.Items.Add(el.Element("temperature").Value));

您可以对XLinq使用XPath扩展:

using System.Xml.Linq;
using System.Xml.XPath;
...
var doc = XDocument.Load("test.xml");
IEnumerable<XElement> dayElements = doc.XPathSelectElements("/info/weather/day/day_part[@type=\"день\"]");
使用System.Xml.Linq;
使用System.Xml.XPath;
...
var doc=XDocument.Load(“test.xml”);
IEnumerable dayElements=doc.XPathSelectElements(“/info/weather/day/day\u part[@type=\”ааа\“]”);

首先-您的输入XML无效,无法加载到
XDocument
。我把它改成:

<?xml version="1.0" encoding="UTF-8"?>
<info lang="ru" xmlns:x="http://www.yandex.ru/xscript">+<region id="213" lon="37.617671" lat="55.755768" zoom="10" />
<traffic lon="37.617671" lat="55.755768" zoom="10" region="213"></traffic>
 <weather region="213" climate="1">
 <source xmlns:xi="http://www.w3.org/2001/XInclude">mb3d</source>
 <day xmlns:xi="http://www.w3.org/2001/XInclude">
  <title>Москва</title>
  <country>Россия</country> 
  <time_zone>Europe/Moscow</time_zone> 
  <summer-time>0</summer-time>  
  <sun_rise>06:51</sun_rise>
  <sunset>20:14</sunset>
  <daytime>d</daytime>
  <date date="2013-04-05T00:00:00Z"></date>
  <day_part type="день" typeid="2">
          <weather_type>облачно</weather_type>
          <weather_code>overcast</weather_code>
          <image>http://weather.yandex.ru/i/5.png</image>
          <image-v2 size="22x22">http://yandex.st/weather/v-1/i/icons/22x22/ovc_+6.png</image-v2>
          <image-v3 size="48x48">http://yandex.st/weather/v-1/i/icons/48x48/ovc.png</image-v3>
          <image_number>5</image_number>
          <wind_speed>5.0</wind_speed>
          <wind_direction id="se">юв</wind_direction>
          <dampness>70</dampness>
          <pressure>743</pressure>
          <temperature color="F2F0E6" class_name="t6">+5</temperature>
          <time_zone>Europe/Moscow</time_zone>
          <observation_time>17:30</observation_time>
          <observation>2013-04-05T17:30:00</observation>
  </day_part>
  <day_part type="вечер" typeid="3"></day_part>
  <day_part type="ночь" typeid="4"></day_part>
  <day_part type="утро" typeid="1"></day_part>
  <day_part type="день" typeid="2"></day_part>
  <night_short></night_short>
  <tomorrow></tomorrow>
</day>
<url xmlns:xi="http://www.w3.org/2001/XInclude">http://pogoda.yandex.ru/moscow/</url>
</weather>
</info> 
对于上面显示的XML文档,
结果
包含一个
字符串
值:
+5

但是,我建议使用
typeid
属性值而不是
type
来搜索
day\u部分
元素:

var results = xDocument.XPathSelectElements("info/weather/day/day_part[@typeid=2]/temperature")
                        .Select(e => (string)e)
                        .ToList();
相同的结果,但由于编码,失败的机会更少

然后,您可以填写
列表框

foreach (var value in results))
{
    listBox1.Items.Add(value);
};

首先,您的XML是无效的!XDocument不会加载这样的xml,它只是xml的一部分。原始文件有效。空引用异常:(如果所有这些示例都给出了空引用异常,那么可能还有其他错误。全天部分元素是否有type属性和temperature元素?我没有注意到xml发生了变化,因此我有空引用异常。因此,输入xml的结构必须与示例xml不同。没有
XML中的元素。+5它不在
day\u部分
element中。谢谢!XML在一天中发生了变化)我没有注意到它)如果change typeid=3,它会得到正确的值!谢谢
var results = xDocument.XPathSelectElements("info/weather/day/day_part[@type='день']/temperature")
                       .Select(e => (string)e)
                       .ToList();
var results = xDocument.XPathSelectElements("info/weather/day/day_part[@typeid=2]/temperature")
                        .Select(e => (string)e)
                        .ToList();
foreach (var value in results))
{
    listBox1.Items.Add(value);
};