Reading an XML Document into the DOM

XML information is read into memory from different formats. It can be read from a string, stream, URL, text reader, or a class derived from the XmlReader.

The Load method brings the document into memory, and has overloaded methods available to take data from each of the different formats. There is also a LoadXML method that reads XML from a string.

Different Load methods affect which nodes are created when the DOM is loaded. The following table lists the differences between some of the Load methods and topics that address these.

Subject Topic
Creation of white space nodes. The object used to load the DOM has an affect on the white space and significant white space nodes generated in the DOM. For more information, see White Space and Significant White Space Handling when Loading the DOM.
Loading XML starting from a specific node or loading the entire XML document. Using the Load method that takes an XmlReader, data can be loaded from a specific node into the DOM. For more information, see Load Data from a Reader.
Validating the XML before it loads. The XmlValidatingReader is the only derived XmlReader that validates XML. For more information, see Validation of XML with XmlValidatingReader.

The following example shows XML being loaded with the LoadXML method, and the data subsequently saved to a text file called data.xml.

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()
        doc.LoadXml(("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>"))
                ' Save the document to a file.
        doc.Save("data.xml")
    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();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    // Save the document to a file.
    doc.Save("data.xml");
  }
}

See Also

XML Document Object Model (DOM)