How can I get all text nodes from an XML file

I want to get all text nodes from an XML file.

How can i do this?

Input Example:

<root> <slide> <Image>hi</Image> <ImageContent>this</ImageContent> <Thumbnail>is</Thumbnail> <ThumbnailContent>A</ThumbnailContent> </slide> </root> 

Expected Result:

 hi this is A 
+4
source share
5 answers

You can try the following:

  string input = @" <root> <slide> <Image>hi</Image> <ImageContent>this</ImageContent> <Thumbnail>is</Thumbnail> <ThumbnailContent>A</ThumbnailContent> </slide> </root>"; XDocument doc = XDocument.Parse(input); //You can also load data from file by passing file path to Load method //XDocument doc = XDocument.Load("Data.xml"); foreach(var slide in doc.Root.Elements("slide")) { var words = slide.Elements().Select(el => el.Value); string s = String.Join(" ", words.ToArray()); } 
+2
source

The only solution (so far) for listing all text nodes in any xml, regardless of its structure:

 string input = @" <root> <slide> <Image>hi</Image> <ImageContent>this</ImageContent> <Thumbnail>is</Thumbnail> <ThumbnailContent>A</ThumbnailContent> </slide> </root>"; foreach (XText text in (IEnumerable)XDocument.Parse(input).XPathEvaluate("//*/text()")) { Console.WriteLine(text.Value); } 

EDIT: if you want to load xml from a file, use XDocument.Load .

+10
source

This code will print the inner text of all xml nodes that do not have a child:

  static void Main(string[] args) { XmlDocument x = new XmlDocument(); x.Load("exp.xml"); PrintNode(x.DocumentElement); } private static void PrintNode(XmlNode x) { if (!x.HasChildNodes) Console.Write(string.Format("{0} ", x.InnerText)); for (int i = 0; i < x.ChildNodes.Count; i++) { PrintNode(x.ChildNodes[i]); } } 

In your XML example, this will produce the desired output :)

+3
source

It will work

 static void Main(string[] args) { XDocument xmlSkuDescDoc = XDocument.Parse (@"<root> <slide> <Image>hi</Image> <ImageContent>this</ImageContent> <Thumbnail>is</Thumbnail> <ThumbnailContent>A</ThumbnailContent> </slide> </root> " ); var result = (from data in xmlSkuDescDoc.Descendants("slide") select data).Elements().Select(i => i.Value).Aggregate((a, b) => a + " " + b); Console.ReadKey(); } 

NB ~ use XDocument.Load(filename) if loading from a file

eg.

 string fileName = @"D:\MyXml.xml"; XDocument xmlSkuDescDoc = XDocument.Load(filename); 

.... and the rest follows as shown above

+2
source

This can be done using the XDocument (LINQ to XML) class. Assuming you have only one slide element:

Using simple XDocument navigation:

 var doc = XDocument.Load("file path here"); if (doc.Root == null) throw new ArgumentException(); // No root node! var slideElement = doc.Root.Element("slide"); if (slideElement == null) throw new ArgumentException(); // No slide node! var values = string.Join(" ", slideElement.Elements().Select(element => element.Value)); 

Using XPath node selection:

 var doc = XDocument.Load("file path here"); var slideElements = doc.XPathSelectElements("root/slide/*"); var values = string.Join(" ", slideElements.Select(element => element.Value)); 
+1
source

All Articles