xelement获取相同的子节点

时间:2014-01-10 07:24:58

标签: c# xml linq xelement

我尝试获取相同的子节点值

我想定期接待孩子

孩子如何属于这个值

XElement element = XElement.Load(filedialog.FileName);

var items = (from el in element.Descendants("property")
select new 
{
   values = el.Attribute("type").Value
}
.ToList();
gridControl.DataSource = items;

示例xml:

<node>
<properties>
  <property type="application">denedim try1</property>
  <property type="direction">in1</property>
  <property type="to">verici verrrrrrr1</property>
  <property type="text_body">patladi yaw1</property>
  <property type="time" year="2013"</property>
  <property type="from_id">6066256731</property>
  <property type="to_id">6700378141</property>
  <property type="status">FREE1</property>
  <property type="uid">1501</property>
  <property type="storage">USB1</property>
</properties>
</node>

<node>
<properties>
  <property type="application">denedim try</property>
  <property type="direction">in</property>
  <property type="to">verici verrrrrrr</property>
  <property type="text_body">patladi yaw</property>
  <property type="time" year="2013"</property>
  <property type="from_id">606625673</property>
  <property type="to_id">670037814</property>
  <property type="status">FREE</property>
  <property type="uid">150</property>
  <property type="storage">USB</property>
</properties>
</node>

2 个答案:

答案 0 :(得分:2)

首先,你错过了')'。接下来,您尝试在XElement中加载xml文档吗?

你可以尝试一下:

List<string> list = new List<string>();
XDocument doc = XDocument.Load(filedialog.FileName);

foreach (XElement elem in doc.Descendants("property"))
{
list.add(elem.Attribute("type").Value);
}
gridControl.DataSource = list;

答案 1 :(得分:0)

元素已加载并包含<node> XElement。 <node>的唯一后代是<properties>。我想你想要这个:

var items = (from el in element.Element("properties").Descendants("property")
    select new
    {
        values = el.Attribute("type").Value
    }).ToList();