Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 592
Colonel Panic Avatar asked Oct 15 '12 14:10

Colonel Panic


People also ask

What is XElement C#?

The XElement class is one of the fundamental classes in LINQ to XML. It represents an XML element. The following list shows what you can use this class for: Create elements. Change the content of the element.

How do you declare XElement in C#?

XElement element = XElement. Load(var); c#


1 Answers

 XElement t = XElement.Parse("<tag>Alice &amp; Bob<other>cat</other></tag>");
 string s = (t.FirstNode as XText).Value;
like image 171
cuongle Avatar answered Sep 26 '22 04:09

cuongle