Read text content from XElement Read text content from XElement xml xml

Read text content from XElement


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


Just because I recently had a similar requirement, I'm offering up:

var x = XElement.Parse("<tag>Alice & Bob<other>cat</other></tag>")var text = string.Concat(x.Nodes().OfType<XText>().Select(t => t.Value));

Will not capture text content of child nodes, but will concatenate all untagged text nodes in the current element.


Try following code It might help you..

namespace ConsoleApplication6{    class Program    {        static void Main(string[] args)        {            var parent = XElement.Parse("<tag>Alice & Bob<other>cat</other></tag>");            var nodes = from x in parent.Nodes()                            where x.NodeType == XmlNodeType.Text                            select (XText)x;            foreach (var val in nodes)            {                Console.WriteLine(val.Value);            }            Console.ReadLine();        }    }}