【发布时间】:2009-12-17 19:34:43
【问题描述】:
假设我有这个 xml:
<categories>
<category text="Arts">
<category text="Design"/>
<category text="Visual Arts"/>
</category>
<category text="Business">
<category text="Business News"/>
<category text="Careers"/>
<category text="Investing"/>
</category>
<category text="Comedy"/>
</categories>
我想编写一个 LINQ 查询,它将返回类别及其父类别(如果有的话)。
例如,如果我正在搜索“商业新闻”,我希望它返回一个包含以下内容的 XElement:
<category text="Business">
<category text="Business News" />
</category>
如果我只搜索“业务”,我只想
<category text="Business" />
到目前为止,我能做的最好的事情是使用 LINQ 来获取我正在搜索的元素,然后检查我找到的节点的父节点是否是根节点并进行相应的调整。有没有更好的办法?
【问题讨论】:
标签: c# xml linq linq-to-xml