This documentation is archived and is not being maintained.

XmlDocument.Load Method (TextReader)

Loads the XML document from the specified TextReader.

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

public virtual void Load (
	TextReader txtReader
)
public void Load (
	TextReader txtReader
)
public function Load (
	txtReader : TextReader
)

Parameters

txtReader

The TextReader 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.

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 uses the StringReader class to load a string of XML data into the XmlDocument object.

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

  }
}

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

public class Sample
{
    public static void main(String[] args)
    {
        // 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.set_InnerText("fantasy");
        doc.get_DocumentElement().AppendChild(elem);

        Console.WriteLine("Display the modified XML...");
        doc.Save(Console.get_Out());
    } //main
} //Sample

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: