Want to return all XElement content as a string

I am new to LINQ in C # and use it to read and work with XML files.

I can move up and down in my elements, but what I want for some elements is to return all the contents as a string. Sense, I have such an element:

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

When I get the value of element1, I want to return all the contents as a text string as follows: "<subel1> some text here </subel1>"

It makes sense? Can anyone help?

(closed as duplicate here )

+3
source share
1 answer

(edit: after the answer, I found the exact duplicate by doing an XElement InnerXml search)

With an XmlDocument that would be simple .InnerXml , but not sure about XElement . Perhaps cheat?

  StringBuilder sb = new StringBuilder(); foreach (var el in foo.Nodes()) sb.AppendLine(el.ToString()); string s = sb.ToString(); 
+1
source

All Articles