使用LINQ to XML查询子节点的内部xml

时间:2009-07-20 22:35:20

标签: c# linq-to-xml

让我说我有这个xml:

<items>
  <item name="thumb">
    <downloadStream>test1</downloadStream>
    <downloadStream>test2</downloadStream>
    <downloadStream>test3</downloadStream>
  </item>
  <item name="photo">
    <downloadStream>test5</downloadStream>
    <downloadStream>test6</downloadStream>
    <downloadStream>test7</downloadStream>
  </item>
</items>

我正在尝试编写一个返回我的LINQ to XML语句:

{“test5”,“test6”,“test7”}

换句话说,它返回每个“downloadStream”节点的内部xml,其中父节点的属性为(name =“photo”)。

我该怎么做?

1 个答案:

答案 0 :(得分:8)

这样的事情:

            var rootElement = XElement.Parse(xml);
            var results = rootElement.
               .Elements()
               .Where( e => e.Attribute("name") == "photo" )
               .SelectMany( e => e.Elements )
               .Select( e => e.Value );