Count the node with whitespace in CSharp

Description

The following code shows how to count the node with whitespace.

Example


/*from  ww  w . j  a  v  a  2  s.c  om*/
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;


public class MainClass{
   public static void Main(){
        XElement xmlTree1 = XElement.Parse("<Root> <Child>  </Child> </Root>", LoadOptions.PreserveWhitespace);
        Console.WriteLine(xmlTree1);
        
        int whiteSpaceNodes;
        
        whiteSpaceNodes = xmlTree1
            .DescendantNodesAndSelf()
            .OfType<XText>()
            .Where(tNode => tNode.ToString().Trim().Length == 0)
            .Count();
        Console.WriteLine("Count of white space nodes (preserving whitespace): {0}", whiteSpaceNodes);
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style