Load Method (XmlReader)

XmlDataDocument.Load Method (XmlReader)

Loads the XmlDataDocument from the specified XmlReader.

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

public override void Load(
	XmlReader reader


Type: System.Xml.XmlReader

XmlReader containing the XML document to load.


The XML being loaded contains entity references, and the reader cannot resolve entities.

XmlDataDocument does not support creating entity references. If the data source contains entity references, you must create an XmlValidatingReader with the EntityHandling property set to EntityHandling.ExpandEntities (this is the default behavior) and pass the XmlValidatingReader to the Load method. If you do not use an XmlValidatingReader, the Load method throws an exception.

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not white space is preserved. The default is false, white space is not preserved.

If the reader is in the initial state (that is, 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", then 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 following siblings look similar to the following:


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


Load will succeed; however, you will have an incomplete DOM tree, because there is no root-level element. You have to add a root-level element before you save the document; otherwise, the Save method throws 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.

The following example modifies the price of a book using the DataSet methods.

using System;
using System.Data;
using System.Xml;
public class Sample {
    public static void Main() {
        // Create an XmlDataDocument.
        XmlDataDocument doc = new XmlDataDocument();

        // Load the schema file.

        // Load the XML data.
        XmlTextReader reader = new XmlTextReader("2books.xml");
        reader.MoveToContent(); // Moves the reader to the root node.

        // Update the price on the first book using the DataSet methods.
        DataTable books = doc.DataSet.Tables["book"];
        books.Rows[0]["price"] = "12.95";

        Console.WriteLine("Display the modified XML data...");
} // End class
#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Xml;

int main() 
   // Create an XmlDataDocument.
   XmlDataDocument* doc = new XmlDataDocument();

   // Load the schema file.

   // Load the XML data.
   XmlTextReader* reader = new XmlTextReader(S"2books.xml");
   reader->MoveToContent(); // Moves the reader to the root node.

   // Update the price on the first book using the DataSet methods.
   DataTable* books = doc->DataSet->Tables->get_Item( S"book" );
   books->Rows->get_Item(0)->set_Item(S"price", S"12.95");

   Console::WriteLine( S"Display the modified XML data...");

The example uses the following two input files.


<!--sample XML fragment-->
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>


<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">
  <xsd:sequence maxOccurs="unbounded">
   <xsd:element name="book"  type="bookType"/>

 <xsd:complexType name="bookType">
   <xsd:element name="title" type="xsd:string"/>
   <xsd:element name="author" type="authorName"/>
   <xsd:element name="price"  type="xsd:decimal"/>
  <xsd:attribute name="genre" type="xsd:string"/>

 <xsd:complexType name="authorName">
   <xsd:element name="first-name"  type="xsd:string"/>
   <xsd:element name="last-name" type="xsd:string"/>


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft