LINQ to xml with nested tags
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 xeleme开发者_如何学Pythonnt obviously returns everything. Thanks for any help.
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:
var textNodes = body.DescendantNodes()
.OfType<XText>()
If you want to get the value of all those nodes concatenated together, you'd do something like:
var text = string.Join("", body.DescendantNodes()
.OfType<XText>()
.Select(x => x.Value)
.ToArray());
(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 with body.Element("FormatStuff")
.
精彩评论