This documentation is archived and is not being maintained.

XmlDocument.Load Method

Loads the specified XML data.

Note   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.

Overload List

Loads the XML document from the specified stream.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Load(Stream)
[C#] public virtual void Load(Stream);
[C++] public: virtual void Load(Stream*);
[JScript] public function Load(Stream);

Loads the XML document from the specified URL.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Load(String)
[C#] public virtual void Load(string);
[C++] public: virtual void Load(String*);
[JScript] public function Load(String);

Loads the XML document from the specified TextReader.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Load(TextReader)
[C#] public virtual void Load(TextReader);
[C++] public: virtual void Load(TextReader*);
[JScript] public function Load(TextReader);

Loads the XML document from the specified XmlReader.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub Load(XmlReader)
[C#] public virtual void Load(XmlReader);
[C++] public: virtual void Load(XmlReader*);
[JScript] public function Load(XmlReader);

Example

[Visual Basic, C#, C++] The following example loads the last book node of the books.xml file into the XML document.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Load. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create the XmlDocument.
        Dim doc As New XmlDocument()
        
        'Load the the document with the last book node.
        Dim reader As 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)
    End Sub 'Main
End Class 'Sample

[C#] 
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);
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
    //Create the XmlDocument.
    XmlDocument* doc = new XmlDocument();

    //Load the the document with the last book node.
    XmlTextReader* reader = new XmlTextReader(S"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);
}

[Visual Basic, C#, C++] 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>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlDocument Class | XmlDocument Members | System.Xml Namespace

Show: