带有嵌套标签的 LINQ to xml
我有一个 xml 文档定义为“
<Body>Stuff stuff stuff <FormatStuff> alsdkfafkafkaf </FormatStuff> </Body>
现在,这显然是有效的 xml”(我不会猜到)。我只想返回 Body 中的信息,以及
的单独 XElement,因此它看起来像
Stuff、Stuff、Stuff
alsdkfafkafkaf
,Body xelement 的 .Value 显然会返回所有内容。感谢您的任何帮助。
I have an xml doc defined as
<Body>Stuff stuff stuff <FormatStuff> alsdkfafkafkaf </FormatStuff> </Body>
Now, this is apparently valid xml (which I wouldn't have guessed). I want to return just the information in Body, and a separate XElement for <FormatStuff>
, so it would look like
Stuff, Stuff, Stuff
alsdkfafkafkaf
the .Value of the Body xelement obviously returns everything. Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么没有猜到这是有效的 XML?老实说,目前还不清楚你想要什么。如果您只想获取 Body 元素的文本节点,您可以使用:
如果您想获取连接在一起的所有这些节点的值,您可以执行以下操作:(
您可以使用节点类型,但随后您只是有一个
IEnumerable
,它没有那么有用,正如我在尝试编译上述内容时发现的那样:)您可以使用
获取
.FormatStuff
元素body.Element("FormatStuff")Why wouldn't you have guessed that this was valid XML? It's not really clear what you want, to be honest. If you just want to get the text nodes for the Body element, you can use:
If you want to get the value of all those nodes concatenated together, you'd do something like:
(You could use the node type, but then you just have an
IEnumerable<XNode>
which isn't as useful, as I found out when trying to compile the above :)You can get the
FormatStuff
element withbody.Element("FormatStuff")
.