tags:

views:

47

answers:

1

Hi everyone,

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.

+2  A: 

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").

Jon Skeet
I had always thought xml had to be hierarchical, a tree. I would think of this a leaf spawning from a leaf. Thanks for the answer!
Steve