-2

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.

4

2 回答 2

3

像这样,也许?

XmlNode[] nodes = ...;
string value = nodes.Single(n => n.LocalName == "Node2").InnerXml;
// or .InnerText, depending on what you need.
于 2012-04-05T16:43:23.517 回答
1

新答案XDocument:根据作者的要求,完全更改为根本不使用:

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();
于 2012-04-05T17:08:21.160 回答