如何执行分层 LINQ to XML 查询?
我知道我可以迭代地执行此操作,但在单个 LINQ 语句中执行此操作会很酷。
我有一些如下所示的 XML:
<parent name="george">
<child name="steve" age="10" />
<child name="sue" age="3" />
<pet type="dog" />
<child name="jill" age="7" />
</parent>
<!-- ... -->
我想编写一个 LINQ to XML 语句将其转换为
<node type="parent" label="george">
<node type="child" label="steve" years="10 />
<node type="child" label="sue" years="3" />
<node type="child" label="jill" years="7" />
<!-- no pets! -->
</parent>
<!-- ... -->
在单个 LINQ to XML 语句中可能吗?
我之前在 LINQ 语句中包含了两个 from
语句,但没有包含第二个 select
,这似乎是这需要的。
I know I can do this iteratively, but it would be cool to do it in a single LINQ statement.
I have some XML that looks like this:
<parent name="george">
<child name="steve" age="10" />
<child name="sue" age="3" />
<pet type="dog" />
<child name="jill" age="7" />
</parent>
<!-- ... -->
and I want to write a LINQ to XML statement to turn it into
<node type="parent" label="george">
<node type="child" label="steve" years="10 />
<node type="child" label="sue" years="3" />
<node type="child" label="jill" years="7" />
<!-- no pets! -->
</parent>
<!-- ... -->
Is that possible in a single LINQ to XML statement?
I've included two from
statements in a LINQ statement before, but not a second select
, which seems to be what this would require.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要查询所需的元素并使用查询的项目创建新的元素和属性。像这样的东西应该有效:
You'll need to query the desired elements and create new elements and attributes using the queried items. Something like this should work:
又快又脏:
Quick and dirty: