This documentation is archived and is not being maintained.

XmlDocument.Load Method (XmlReader)

Loads the XML document from the specified XmlReader.

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

public virtual void Load (
	XmlReader reader
)
public void Load (
	XmlReader reader
)
public function Load (
	reader : XmlReader
)

Parameters

reader

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

Exception typeCondition

XmlException

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

NoteNote

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:

<!--comment--><element1>one</element1><element2>two</element2>

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.

This method does not do DTD or schema validation. If you want validation to occur, you need to pass an XmlValidatingReader, specify the appropriate XmlValidatingReader.ValidationType, and provide a XmlValidatingReader.ValidationEventHandler. You are then notified of all validation errors found during Load, and unless your ValidationEventHandler throws an exception to stop the process, the document will still be loaded. See XmlDocument for an example of load-time validation

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.MoveToContent();
    reader.Read();
    reader.Skip(); //Skip the first book.
    reader.Skip(); //Skip the second book.
    doc.Load(reader);

    doc.Save(Console.Out);
  }
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        //Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

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

        doc.Save(Console.get_Out());
    } //main
} //Sample

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

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: