Bewerken

Delen via


XmlDocument.Load Method

Definition

Loads the specified XML data from a Stream, a URL, a TextReader, or an XmlReader.

Overloads

Load(Stream)

Loads the XML document from the specified stream.

Load(TextReader)

Loads the XML document from the specified TextReader.

Load(String)

Loads the XML document from the specified URL.

Load(XmlReader)

Loads the XML document from the specified XmlReader.

Load(Stream)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified stream.

public:
 virtual void Load(System::IO::Stream ^ inStream);
public virtual void Load (System.IO.Stream inStream);
abstract member Load : System.IO.Stream -> unit
override this.Load : System.IO.Stream -> unit
Public Overridable Sub Load (inStream As Stream)

Parameters

inStream
Stream

The stream containing the XML document to load.

Exceptions

There is a load or parse error in the XML. In this case, a FileNotFoundException is raised.

Remarks

Note

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

This method automatically detects the string format of the input XML (for example, UTF-8, ANSI, and so on). If your application needs to know which encoding is used to read the stream, consider using an XmlTextReader object to read the stream, and then use the XmlTextReader.Encoding property to determine the encoding. If you need to use a XmlDocument object to work with XML, you can use the XmlTextReader object to create one. For more information, see Reading XML Data using XPathDocument and XmlDocument.

See also

Applies to

Load(TextReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified TextReader.

public:
 virtual void Load(System::IO::TextReader ^ txtReader);
public virtual void Load (System.IO.TextReader txtReader);
abstract member Load : System.IO.TextReader -> unit
override this.Load : System.IO.TextReader -> unit
Public Overridable Sub Load (txtReader As TextReader)

Parameters

txtReader
TextReader

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

Exceptions

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

Examples

The following example uses the StringReader class to load a string of XML data into the XmlDocument object.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   // Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   String^ xmlData = "<book xmlns:bk='urn:samples'></book>";
   doc->Load( gcnew StringReader( xmlData ) );
   
   // Create a new element and add it to the document.
   XmlElement^ elem = doc->CreateElement( "bk", "genre", "urn:samples" );
   elem->InnerText = "fantasy";
   doc->DocumentElement->AppendChild( elem );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

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

public class Sample {

  public static void Main() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    string xmlData = "<book xmlns:bk='urn:samples'></book>";

    doc.Load(new StringReader(xmlData));

    // Create a new element and add it to the document.
    XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples");
    elem.InnerText = "fantasy";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main() 

    ' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim xmlData as string = "<book xmlns:bk='urn:samples'></book>"

    doc.Load(new StringReader(xmlData))

    ' Create a new element and add it to the document.
    Dim elem as XmlElement = doc.CreateElement("bk", "genre", "urn:samples")
    elem.InnerText = "fantasy"
    doc.DocumentElement.AppendChild(elem)

    Console.WriteLine("Display the modified XML...")
    doc.Save(Console.Out)

  end sub
end class

Remarks

Note

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

See also

Applies to

Load(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified URL.

public:
 virtual void Load(System::String ^ filename);
public virtual void Load (string filename);
abstract member Load : string -> unit
override this.Load : string -> unit
Public Overridable Sub Load (filename As String)

Parameters

filename
String

URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).

Exceptions

There is a load or parse error in the XML. In this case, a FileNotFoundException is raised.

filename is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

filename is null.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

filename specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

filename specified a directory.

-or-

The caller does not have the required permission.

The file specified in filename was not found.

filename is in an invalid format.

The caller does not have the required permission.

Remarks

Note

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

See also

Applies to

Load(XmlReader)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Loads the XML document from the specified XmlReader.

public:
 virtual void Load(System::Xml::XmlReader ^ reader);
public virtual void Load (System.Xml.XmlReader reader);
abstract member Load : System.Xml.XmlReader -> unit
override this.Load : System.Xml.XmlReader -> unit
Public Overridable Sub Load (reader As XmlReader)

Parameters

reader
XmlReader

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

Exceptions

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

Examples

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew XmlDocument;
   
   //Load the document with the last book node.
   XmlTextReader^ reader = gcnew 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 );
}

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

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

    //Load 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);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

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

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>

Remarks

Note

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.

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

See also

Applies to