Search code examples
c#xmllinqxmldocument

Searching an array of XML Nodes


I have an array of System.Xml.XmlNode with data similar to this:

[0] = <Node1 xmlns="---">N1Data</Node1>

[1] = <Node2 xmlns="---">N2Data</Node2>

[2] = <Node3 xmlns="---">N3Data</Node3>

Using LINQ, how could I select the inner data of Node2? This seems trivial with an XDocument, but my data format is nonnegotiable as it's supplied by an external resource.

Thanks in advance.


Solution

  • Like this, perhaps?

    XmlNode[] nodes = ...;
    string value = nodes.Single(n => n.LocalName == "Node2").InnerXml;
    // or .InnerText, depending on what you need.