如何获取 List来自这个 XML?

发布于 2024-09-07 10:41:26 字数 789 浏览 5 评论 0原文

我有一个类似于以下内容的 XML 块:

<factsheet>
<bilcontents >
  <config name="1" />
</bilcontents>

<singlefactsheet includeInBook="true">
  <config name="7" />
  <fund id="630" />
</singlefactsheet>

<doubleFactsheet includeInBook="true">
  <config name="8" />
  <fund id="623" />
  <fund id="624" />
</doubleFactsheet>

<tripplefactsheet includeInBook="true">
  <config name="6" />
  <fund id="36" />
  <fund id="37" />
  <fund id="38" />
</tripplefactsheet>
</factsheet>

Is it possible for me to get a list of XElements that contains every XElements, as long as includeInBook="true", and with every element I can然后根据节点的类型进行处理。

I have a block of XML similar to the following:

<factsheet>
<bilcontents >
  <config name="1" />
</bilcontents>

<singlefactsheet includeInBook="true">
  <config name="7" />
  <fund id="630" />
</singlefactsheet>

<doubleFactsheet includeInBook="true">
  <config name="8" />
  <fund id="623" />
  <fund id="624" />
</doubleFactsheet>

<tripplefactsheet includeInBook="true">
  <config name="6" />
  <fund id="36" />
  <fund id="37" />
  <fund id="38" />
</tripplefactsheet>
</factsheet>

Is it possible for me to get a list of XElements that contains each of these, as long as includeInBook="true", and with each element I can then deal with it based on the type of node.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

智商已欠费 2024-09-14 10:41:26

绝对:

 var list = doc.Root
               .Elements()
               .Where(x => (bool?) x.Attribute("includeInBook") == true)
               .ToList();

顺便说一句,处理可为空的布尔值可能有点奇怪。 lambda 表达式的另一种方法可以是:

x => (bool?) x.Attribute("includeInBook") ?? false

或者

x => (string) x.Attribute("includeInBook") == "true"

Absolutely:

 var list = doc.Root
               .Elements()
               .Where(x => (bool?) x.Attribute("includeInBook") == true)
               .ToList();

Dealing with nullable Booleans can be a little odd, btw. An alternative approach for the lambda expression could be:

x => (bool?) x.Attribute("includeInBook") ?? false

or

x => (string) x.Attribute("includeInBook") == "true"
昇り龍 2024-09-14 10:41:26

这就是我的成果,尽管乔恩几乎抢先一步

var inBook = nodes.Descendants()
    .Where(xx => xx.Attribute("includeInBook") != null)
    .Select(xx => xx).ToList();

This is what I worked out, though Jon pretty much beat me to the punch

var inBook = nodes.Descendants()
    .Where(xx => xx.Attribute("includeInBook") != null)
    .Select(xx => xx).ToList();
爱她像谁 2024-09-14 10:41:26

您应该考虑为此 XML 创建数据结构并使用“序列化”。这将是与该数据模型交互的更简洁的方式。只是需要考虑的事情...

You should consider creating data structures for this XML and using "serialization". This would be a much cleaner way to interact with this data model. Just something to consider...

弱骨蛰伏 2024-09-14 10:41:26

或者,您可以使用内置的 XPath 扩展方法(它们位于 System.Xml.Xpath 中:

XDocument doc;
// ...
var includeInBook = doc.XPathSelectElements("/factsheet/*[@includeInBook = 'true']")

读为:

  • /factsheet:选择元素 'factsheet'
  • < code>/*:然后选择所有子元素
  • [ ... ]:将方括号读作“where”
    • @includeInBook = 'true':属性“includeInBook”的内容等于“true”

Alternately, you could use the built-in XPath extension methods methods (they live in System.Xml.Xpath:

XDocument doc;
// ...
var includeInBook = doc.XPathSelectElements("/factsheet/*[@includeInBook = 'true']")

Read as:

  • /factsheet: select the element 'factsheet'
  • /*: then choose all children
  • [ ... ]: read square brackets as "where"
    • @includeInBook = 'true': the attribute "includeInBook" has content equal to 'true'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文