Creates an XmlReader from XDocument in CSharp

Description

The following code shows how to creates an XmlReader from XDocument.

Example


using System;//from w  w  w .  j a v a  2s.  c o m
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass{
   public static void Main(){
        XDocument xmlTree = new XDocument(
            new XElement("Root",
                new XAttribute("Att1", "Attribute Content"),
                new XElement("A1", 1),
                new XElement("A2", 2)
            )
        );
        XmlReader reader = xmlTree.CreateReader();
        reader.MoveToContent();
        XmlDocument doc = new XmlDocument();
        XmlNode cd = doc.ReadNode(reader);
        doc.AppendChild(cd);
        Console.WriteLine(doc.OuterXml);
   }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style