Load xml file with option in CSharp
Description
The following code shows how to load xml file with option.
Example
// w ww .ja v a 2 s.co m
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);
xmlTree1.Save("Tree.xml");
Console.WriteLine(xmlTree1);
int whiteSpaceNodes;
XElement xmlTree2 = XElement.Load("Tree.xml",LoadOptions.None);
whiteSpaceNodes = xmlTree2
.DescendantNodesAndSelf()
.OfType<XText>()
.Where(tNode => tNode.ToString().Trim().Length == 0)
.Count();
Console.WriteLine("Count of white space nodes (not preserving whitespace): {0}", whiteSpaceNodes);
}
}
The code above generates the following result.