Query DescendantNodes And Self in CSharp
Description
The following code shows how to query DescendantNodes And Self.
Example
//from w ww . j a va 2 s . c o m
using System;
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);
}
}