Read text content from XElement

Colonel Panic picture Colonel Panic · Oct 15, 2012 · Viewed 13.4k times · Source

In .NET, how do I read the text content from an XElement?

For example, from the XElement

XElement.Parse("<tag>Alice &amp; Bob<other>cat</other></tag>")

I would like the string 'Alice & Bob'


I tried element.Value but that returns 'Alice & Bobcat' :(

Answer

cuongle picture cuongle · Oct 15, 2012
 XElement t = XElement.Parse("<tag>Alice &amp; Bob<other>cat</other></tag>");
 string s = (t.FirstNode as XText).Value;