Export (0) Print
Expand All

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

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.

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 Validating XML Data with XmlReader.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft