System.XML 将值读入数组

System.XML Read values into an array

我需要将 XML 中的所有文本值读入列表...

我的 XML 具有以下格式:

<MultiNodePicker type="content">
  <nodeId>52515</nodeId>
  <nodeId>52519</nodeId>
</MultiNodePicker>

我的代码:

string mystring= @"<MultiNodePicker type='content'>
  <nodeId>52515</nodeId>
  <nodeId>52519</nodeId>
</MultiNodePicker>";
var doc = new XmlDocument();
doc.LoadXml(mystring);
Console.WriteLine(doc.InnerText);  
List<string> ids = doc.GetTextValues???()

使用一点 LINQ:

var ids = XElement.Parse(mystring)
    .Descendants("nodeId")
    .Select(x => x.Value); // or even .Select(x => int.Parse(x.Value));

foreach(var id in ids) {
    Console.WriteLine(id);
}

使用 LinQ XML:

string mystring = @"<MultiNodePicker type='content'>
<nodeId>52515</nodeId>
<nodeId>52519</nodeId>
</MultiNodePicker>";
var doc = new XmlDocument();
doc.LoadXml(mystring);
List<string> memberNames = XDocument.Parse(mystring)
.XPathSelectElements("//MultiNodePicker/nodeId")
.Select(x => x.Value)
.ToList();