System.Xml.XmlDocument.Load Method

Loads the XML document from the specified System.Xml.XmlReader.

Syntax

public virtual void Load (XmlReader reader)

Parameters

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

Remarks

Note:

The Load method always preserves significant white space. The XmlDocument.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 (XmlReader.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:

Example

<!--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:

Example

<!--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 XmlDocument.Save(string) 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 System.Xml.XmlReader instance by using the System.Xml.XmlReaderSettings class and the erload:System.Xml.XmlReader.Create method. For more information, see Validating XML Data with XmlReader.

This method is a Microsoft extension to the Document Object Model (DOM).

Requirements

Namespace: System.Xml
Assembly: System.Xml (in System.Xml.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0