views:

433

answers:

1

I'm a newbie to LINQ in C# and am using it to read in and work with XML files.

I'm able to navigate up and down my elements, but what I want for some elements is to return the entire contents as a string. Meaning, I have an element like this:

<element1>
    <subel1> some text here </subel1>
</element1>

When I get the value of element1, I want to get back the entire contents as a text string like this: "<subel1> some text here </subel1>"

Does that make sense? Can anyone help?

(closed as duplicate of here)

A: 

(edit: after replying, I found an exact duplicate, by searching "XElement InnerXml")

With XmlDocument that would be just .InnerXml - not sure about XElement, though. Perhaps cheat?

    StringBuilder sb = new StringBuilder();
    foreach (var el in foo.Nodes()) sb.AppendLine(el.ToString());
    string s = sb.ToString();
Marc Gravell
That worked perfectly, thanks!-Adeena
adeena