test1 <...">

Using LINQ to XML to query inner xml of child nodes

Say I have this 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>

I am trying to write a LINQ to XML statement that returns me:

{"test5", "test6", "test7"}

In other words, it returns me the internal xml for each "downloadStream" node, where the parent node has an attribute (name = "photo").

How to do it?

+3
source share
1 answer

Something like that:

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

Source: https://habr.com/ru/post/1713105/


All Articles