I am parsing an XML structure and my classes look like the following:
class MyXml
{
//...
List<Node> Content { get; set; }
//...
}
class Node
{
// ...
public List<Node> Nodes { get; set; }
public string Type { get; set; }
//...
}
MyXml represents the XML file I am parsing, whose elements are all called <node>
. Each node has a type attribute, which can have different values.
The type of the node is not connected to its depth. I can have any node type at any depth level.
I can parse the structure correctly, so I get a MyXml object whose content is a list of Nodes, where ever node in the List can have subnodes and so on (I used recursion for that).
What I need to do is flatten this whole structure and extract only the nodes of a certain type.
I tried with:
var query = MyXml.Content.SelectMany(n => n.Nodes);
but it's taking only the nodes with a structure depth of 1. I would like to grab every node, regardless of depth, in the same collection and then filter what I need.