Load XML string with XDocument

In this chapter you will learn:

  1. How to load XML string with XDocument

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:

  1. How to access XDocument Root
Home » C# Tutorial » XML Linq
XDocument
Create XDocument
Add to XDocument
Parse XML file with XDocument
Load XML string with XDocument
XDocument Root
Query XML document with Linq
Save XML document
XDocument Serialize
XElement namespace
Adding attribute
XElement's NextNode
XAttribute
XAttribute value
XAttribute Properties
XAttribute namespace
XComment
XDeclaration
XML declaration
XCData
XNamespace