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.
Like this, perhaps?
XmlNode[] nodes = ...;
string value = nodes.Single(n => n.LocalName == "Node2").InnerXml;
// or .InnerText, depending on what you need.
New Answer: Completely changed to not use XDocument
at all, per author's request:
string[] elementArray = new[]
{
"<Node1 xmlns=\"foo\">Bar</Node1>",
"<Node2 xmlns=\"foo\">Bar</Node2>",
"<Node3 xmlns=\"foo\">Bar</Node3>"
};
var search = "Node2";
string result = elementArray
.Where(x => x.Split(' ').First().Substring(1) == search)
.Select(x =>
{
int closeBrace = x.IndexOf(">");
int openBrace = x.IndexOf("<", closeBrace);
return x.Substring(closeBrace + 1, openBrace - closeBrace - 1);
})
.Single();