【问题标题】:Parsing XDocument using LINQ使用 LINQ 解析 XDocument
【发布时间】:2012-03-06 10:14:27
【问题描述】:

我正在对XDocument 进行以下查询。最后一级.Descendants("Instance") 产生一个 XElement 列表,其形式如下:

<Instance>filepath1</Instance>
<Instance>filepath2</Instance>
<Instance>filepath3</Instance>
<Instance>filepath4</Instance>

查询

 List<string> fileNames = xDoc.Descendants("Main")
                       .FirstOrDefault()
                       .Descendants("SecondLevel")
                       .FirstOrDefault()
                       .Descendants("Instance")
                       .Select().ToList(); //this line is not correct. Need to get the instances node values as List<string>

如何将值filepath1filepath2.. 存储在List&lt;string&gt; 中?

【问题讨论】:

  • 类似Select(x=&gt;x.Value) ?

标签: c# xml linq


【解决方案1】:

通过使用

 ....
  .Descendants("Instance")
  .Select(e => e.Value) // project to the string values
  .ToList();

【讨论】:

  • 谢谢,它成功了。原谅我的无知,那是什么语法。看起来像用 Lamda 表达式写匿名函数。
  • 这是一个 lambda 表达式。它将找到的 XElement 转换为它们的字符串值。阅读基本的 LINQ,任何教程都可以。
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多