XmlDocument Class
Represents an XML document in memory.
System.Xml.XmlDocument
Inheritance
System.ObjectSystem.Xml.XmlNodeSystem.Xml.XmlDocument
Remarks
The XmlDocument class represents an XML document as a tree of nodes. It allows you to load, navigate, modify, and save XML documents. It implements the System.Xml.XmlLoadable interface, providing methods for parsing XML from various sources.
XmlDocument is a convenient class for working with XML in memory. However, for very large XML documents, consider using XmlReader and XmlWriter for more efficient, stream-based processing to reduce memory usage.
XmlDocument.CreateNavigator method to obtain an XPathNavigator object for navigating the XML document using XPath queries.
Constructors
XmlDocument()
Initializes a new instance of the XmlDocument class.
public XmlDocument();
Properties
| Name | Description |
|---|---|
DocumentElement |
Gets the XmlElement that represents the root element of the document. |
DocumentType |
Gets the XmlDocumentType node. |
IsReadOnly |
Gets a value indicating whether this node is read-only. |
LocalName |
Gets the qualified name of the current node. |
Name |
Gets the name of the current node. |
NodeType |
Gets the node type of the current node. |
OwnerDocument |
Gets the XmlDocument to which this node belongs. |
SchemaInfo |
Gets XmlSchemaInfo information for the current node. |
Supports |
Gets a value indicating whether the DOM supports a specific feature. |
Value |
Gets or sets the value of the current node. |
XmlResolver |
Gets or sets the XmlResolver used for resolving external entities. |
Methods
Load(string filename)
Loads the XML document from the specified file.
public void Load(string filename);
LoadXml(string xml)
Loads the XML document from the specified string.
public void LoadXml(string xml);
Save(string filename)
Saves the XML document to the specified file.
public void Save(string filename);
CreateElement(string tagName)
Creates an XmlElement with the specified name.
public XmlElement CreateElement(string tagName);
CreateAttribute(string attributeName)
Creates an XmlAttribute with the specified name.
public XmlAttribute CreateAttribute(string attributeName);
CreateTextNode(string text)
Creates an XmlText node with the specified text.
public XmlText CreateTextNode(string text);
Example
The following example demonstrates how to load an XML document from a string, find an element, and then save it to a file.
using System;
using System.Xml;
public class Example
{
public static void Main(string[] args)
{
// Load XML from a string
XmlDocument doc = new XmlDocument();
doc.LoadXml("<bookstore><book genre=\"fiction\"><title lang=\"en\">The Lord of the Rings</title><author>J.R.R. Tolkien</author></book></bookstore>");
// Get the root element
XmlElement root = doc.DocumentElement;
// Create a new element
XmlElement newBook = doc.CreateElement("book");
newBook.SetAttribute("genre", "fantasy");
XmlElement newTitle = doc.CreateElement("title");
newTitle.SetAttribute("lang", "en");
newTitle.InnerText = "The Hobbit";
newBook.AppendChild(newTitle);
XmlElement newAuthor = doc.CreateElement("author");
newAuthor.InnerText = "J.R.R. Tolkien";
newBook.AppendChild(newAuthor);
// Append the new book to the bookstore
root.AppendChild(newBook);
// Save the modified XML to a file
doc.Save("books.xml");
Console.WriteLine("XML document loaded, modified, and saved to books.xml");
}
}