XmlDocument Class

System.Xml Namespace

On this page

Introduction

Represents an XML document. The XmlDocument class provides a programmatic representation of an XML document and a way to navigate, manipulate, and save the document. It implements the System.Xml.XmlNode interface and is the main way to work with XML data in memory.

XmlDocument provides a DOM (Document Object Model) view of the XML document. This allows for easy access to all parts of the XML document, including elements, attributes, and text nodes.

Declaration

public class XmlDocument
    : XmlNode
// Inherited members
// ...

Syntax

public class XmlDocument : XmlNode

Remarks

The XmlDocument class is the central component for working with XML documents using the .NET Framework's XML Object Model (DOM). It provides a tree-based representation of an XML document, allowing you to load, parse, modify, and save XML data.

Key features include:

  • Loading XML from strings, files, and streams.
  • Accessing and manipulating the document's nodes (elements, attributes, text, etc.).
  • Creating new nodes and inserting them into the document structure.
  • Saving the modified XML document to various destinations.
  • Validation against DTDs and XML Schemas (though this is often handled by other classes like XmlReaderSettings).

It is important to note that XmlDocument loads the entire XML document into memory. For very large XML files, consider using streaming APIs like XmlReader or XmlWriter to avoid excessive memory consumption.

Constructors

XmlDocument()

public XmlDocument();

Initializes a new instance of the XmlDocument class with default settings. This creates an empty XML document.

Properties

DocumentElement

public XmlElement? DocumentElement { get; }

Gets the root element of the document. Returns null if the document has no root element.

IsEmpty

public bool IsEmpty { get; }

Determines whether the current XML document is empty.

NodeType

public XmlNodeType NodeType { get; }

Gets the type of the current node, which is always XmlNodeType.Document for an XmlDocument.

Methods

Load(string uri)

public void Load(string uri);

Loads the XML document from the specified URI.

LoadXml(string xml)

public void LoadXml(string xml);

Loads the XML document from the specified string.

CreateElement(string tagName)

public XmlElement CreateElement(string tagName);

Creates an XmlElement with the specified tag name.

Save(string fileName)

public void Save(string fileName);

Saves the XML document to the specified file.

Examples

Loading and Modifying an XML Document


using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a new XML document
        XmlDocument doc = new XmlDocument();

        // Load XML from a string
        string xmlString = @"

    
        The Hitchhiker's Guide to the Galaxy
        Douglas Adams
    
";
        doc.LoadXml(xmlString);

        // Get the root element
        XmlElement root = doc.DocumentElement;
        if (root != null)
        {
            // Create a new element
            XmlElement newBook = doc.CreateElement("book");
            newBook.SetAttribute("category", "science");

            XmlElement title = doc.CreateElement("title");
            title.SetAttribute("lang", "en");
            title.InnerText = "A Brief History of Time";
            newBook.AppendChild(title);

            XmlElement author = doc.CreateElement("author");
            author.InnerText = "Stephen Hawking";
            newBook.AppendChild(author);

            // Append the new book to the root
            root.AppendChild(newBook);

            // Save the modified document to a file
            doc.Save("books.xml");
            Console.WriteLine("XML document saved to books.xml");
        }
    }
}