Load XML string with XDocument
In this chapter you will learn:
How to load XML string with XDocument
using System;/* j ava2s. c o m*/
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass
{
public static void Main()
{
TextReader sr;
int whiteSpaceNodes;
sr = new StringReader("<Root> <Child> </Child> </Root>");
XDocument xmlTree2 = XDocument.Load(sr, LoadOptions.PreserveWhitespace);
sr.Close();
whiteSpaceNodes = xmlTree2
.Element("Root")
.DescendantNodesAndSelf()
.OfType<XText>()
.Where(tNode => tNode.ToString().Trim().Length == 0)
.Count();
Console.WriteLine(whiteSpaceNodes);
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » XML Linq