如何获取 List来自这个 XML?
我有一个类似于以下内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
绝对:
顺便说一句,处理可为空的布尔值可能有点奇怪。 lambda 表达式的另一种方法可以是:
或者
Absolutely:
Dealing with nullable Booleans can be a little odd, btw. An alternative approach for the lambda expression could be:
or
这就是我的成果,尽管乔恩几乎抢先一步
This is what I worked out, though Jon pretty much beat me to the punch
您应该考虑为此 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...
或者,您可以使用内置的 XPath 扩展方法(它们位于 System.Xml.Xpath 中:
读为:
/factsheet
:选择元素 'factsheet'[ ... ]
:将方括号读作“where”@includeInBook = 'true'
:属性“includeInBook”的内容等于“true”Alternately, you could use the built-in XPath extension methods methods (they live in
System.Xml.Xpath
: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'