Search code examples
c#.netxmllinqxelement

Read text content from XElement


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' :(


Solution

  •  XElement t = XElement.Parse("<tag>Alice &amp; Bob<other>cat</other></tag>");
     string s = (t.FirstNode as XText).Value;