XmlDocument.Load Method (XmlReader)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Loads the XML document from the specified XmlReader.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public virtual void Load(
	XmlReader reader


Type: System.Xml.XmlReader

The XmlReader used to feed the XML data into the document.

Exception Condition

There is a load or parse error in the XML. In this case, the document remains empty.


The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

If the reader is in the initial state (ReadState =ReadState.Initial), Load consumes the entire contents of the reader and builds the DOM from what it finds.

If the reader is already positioned on some node at depth "n", this method loads that node and all subsequent siblings up to the end tag that closes depth "n". This has the following results.

If the current node and its siblings look like the following:


Load throws an exception because a document cannot have two root level elements. If the current node and its siblings look like the following:

<!--comment--><?process instruction?><!--comment--></endtag>

Load succeeds, but you have an incomplete DOM tree because there is no root level element. Before you save the document, you must add a root level element, otherwise Save will throw an exception.

If the reader is positioned on a leaf node that is invalid for the root level of a document, for example a white space or attribute node, the reader continues to read until it is positioned on a node that can be used for the root. The document begins loading at this point.

If you want validation to occur, you can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page.

This method is a Microsoft extension to the Document Object Model (DOM).

The following example loads the last book node of the books.xml file into the XML document.

using System;
using System.IO;
using System.Xml;

public class Sample
  public static void Main()
    //Create the XmlDocument.
    XmlDocument doc = new XmlDocument();

    //Load the the document with the last book node.
    XmlTextReader reader = new XmlTextReader("books.xml");
    reader.WhitespaceHandling = WhitespaceHandling.None;
    reader.Skip(); //Skip the first book.
    reader.Skip(); //Skip the second book.


The example uses the file, books.xml, as input.

<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top