What we have in this page?
Using XmlDocument
Our handling of XML so far has been forward-only, which is very light on resource usage but isn’t so useful if you need to move around within the XML document. The XmlDocument class is based on the W3C DOM, and it’s the class that you want to use if you need to browse, modify, or create an XML document.
What Is the W3C DOM?
The DOM is a specification for an API that lets programmers manipulate XML held in memory. The DOM specification is language-independent, and bindings are available for many programming languages, including C++. XmlDocument is based on the DOM, with Microsoft extensions. Because XmlDocument works with XML in memory, it has several advantages and disadvantages when compared with the XmlTextReader forward-only approach. One advantage is that, in reading the entire document and building a tree in memory, you have access to all the elements and can wander through the document at will. You can also edit the document by changing, adding, or deleting nodes, and you can write the changed document back to disk again. It’s even possible to create an entire XML document from scratch in memory and write it out, serialize it, which is a useful alternative to using XmlTextWriter. The main disadvantage is that all of an XML document is held in memory at once, so the amount of memory needed by your program is going to be proportional to the size of the XML document you’re working with. Therefore, if you’re working with a very large XML document or have limited memory you might not be able to use XmlDocument. The XmlDocument class has a number of properties, methods, and events and the following tables list the members exposed by the XmlDocument type. |
Public Constructors | |
Name | Description |
Symbol | |
XmlDocument | Overloaded. Initializes a new instance of the XmlDocument class. |
Table 15. |
Protected Constructors | |
Name | Description |
Symbol | |
XmlDocument | Overloaded. Initializes a new instance of the XmlDocument class. |
Table 16. |
Public Properties | |
Name | Description |
Symbol | |
Attributes | Gets an XmlAttributeCollection containing the attributes of this node. (Inherited from XmlNode.) |
BaseURI | Overridden. Gets the base URI of the current node. |
ChildNodes | Gets all the child nodes of the node. (Inherited from XmlNode.) |
DocumentElement | Gets the root XmlElement for the document. |
DocumentType | Gets the node containing the DOCTYPE declaration. |
FirstChild | Gets the first child of the node. (Inherited from XmlNode.) |
HasChildNodes | Gets a value indicating whether this node has any child nodes. (Inherited from XmlNode.) |
Implementation | Gets the XmlImplementation object for the current document. |
InnerText | Gets or sets the concatenated values of the node and all its child nodes. (Inherited from XmlNode.) |
InnerXml | Overridden. Gets or sets the markup representing the children of the current node. |
IsReadOnly | Overridden. Gets a value indicating whether the current node is read-only. |
Item | Overloaded. Gets the specified child element. (Inherited from XmlNode.) |
LastChild | Gets the last child of the node. (Inherited from XmlNode.) |
LocalName | Overridden. Gets the local name of the node. |
Name | Overridden. Gets the qualified name of the node. |
NamespaceURI | Gets the namespace URI of this node. (Inherited from XmlNode.) |
NameTable | Gets the XmlNameTable associated with this implementation. |
NextSibling | Gets the node immediately following this node. (Inherited from XmlNode.) |
NodeType | Overridden. Gets the type of the current node. |
OuterXml | Gets the markup representing this node and all its child nodes. (Inherited from XmlNode.) |
OwnerDocument | Overridden. Gets the XmlDocument to which the current node belongs. |
ParentNode | Overridden. Gets the parent node of this node (for nodes that can have parents). |
Prefix | Gets or sets the namespace prefix of this node. (Inherited from XmlNode.) |
PreserveWhitespace | Gets or sets a value indicating whether to preserve white space in element content. |
PreviousSibling | Gets the node immediately preceding this node. (Inherited from XmlNode.) |
SchemaInfo | Overridden. Returns the Post-Schema-Validation-Infoset (PSVI) of the node. |
Schemas | Gets or sets the XmlSchemaSet object associated with this XmlDocument. |
Value | Gets or sets the value of the node. (Inherited from XmlNode.) |
XmlResolver | Sets the XmlResolver to use for resolving external resources. |
Table 17. |
Public Methods | |
Name | Description |
Symbol | |
AppendChild | Adds the specified node to the end of the list of child nodes, of this node. (Inherited from XmlNode.) |
Clone | Creates a duplicate of this node. (Inherited from XmlNode.) |
CloneNode | Overridden. Creates a duplicate of this node. |
CreateAttribute | Overloaded. Creates an XmlAttribute with the specified name. |
CreateCDataSection | Creates an XmlCDataSection containing the specified data. |
CreateComment | Creates an XmlComment containing the specified data. |
CreateDocumentFragment | Creates an XmlDocumentFragment. |
CreateDocumentType | Returns a new XmlDocumentType object. |
CreateElement | Overloaded. Creates an XmlElement. |
CreateEntityReference | Creates an XmlEntityReference with the specified name. |
CreateNavigator | Overloaded. Overridden. Creates a new XPathNavigator object for navigating this document. |
CreateNode | Overloaded. Creates an XmlNode. |
CreateProcessingInstruction | Creates an XmlProcessingInstruction with the specified name and data. |
CreateSignificantWhitespace | Creates an XmlSignificantWhitespace node. |
CreateTextNode | Creates an XmlText with the specified text. |
CreateWhitespace | Creates an XmlWhitespace node. |
CreateXmlDeclaration | Creates an XmlDeclaration node with the specified values. |
Equals | Overloaded. Determines whether two Object instances are equal. (Inherited from Object.) |
GetElementById | Gets the XmlElement with the specified ID. |
GetElementsByTagName | Overloaded. Returns an XmlNodeList containing a list of all descendant elements that match the specified name. |
GetEnumerator | Provides support for the for each style iteration over the nodes in the XmlNode. (Inherited from XmlNode.) |
GetHashCode | Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.) |
GetNamespaceOfPrefix | Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the namespace URI in the declaration. (Inherited from XmlNode.) |
GetPrefixOfNamespace | Looks up the closest xmlns declaration for the given namespace URI that is in scope for the current node and returns the prefix defined in that declaration. (Inherited from XmlNode.) |
GetType | Gets the Type of the current instance. (Inherited from Object.) |
ImportNode | Imports a node from another document to the current document. |
InsertAfter | Inserts the specified node immediately after the specified reference node. (Inherited from XmlNode.) |
InsertBefore | Inserts the specified node immediately before the specified reference node. (Inherited from XmlNode.) |
Load | Overloaded. Loads the specified XML data. |
LoadXml | Loads the XML document from the specified string. |
Normalize | Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes. (Inherited from XmlNode.) |
PrependChild | Adds the specified node to the beginning of the list of child nodes for this node. (Inherited from XmlNode.) |
ReadNode | Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute. |
ReferenceEquals | Determines whether the specified Object instances are the same instance. (Inherited from Object.) |
RemoveAll | Removes all the child nodes and/or attributes of the current node. (Inherited from XmlNode.) |
RemoveChild | Removes specified child node. (Inherited from XmlNode.) |
ReplaceChild | Replaces the child node oldChild with newChild node. (Inherited from XmlNode.) |
Save | Overloaded. Saves the XML document to the specified location. |
SelectNodes | Overloaded. Selects a list of nodes matching the XPath expression. (Inherited from XmlNode.) |
SelectSingleNode | Overloaded. Selects the first XmlNode that matches the XPath expression. (Inherited from XmlNode.) |
Supports | Test if the DOM implementation implements a specific feature. (Inherited from XmlNode.) |
ToString | Returns a String that represents the current Object. (Inherited from Object.) |
Validate | Overloaded. Validates the XmlDocument against the XML Schema Definition Language (XSD) schemas contained in the Schemas property. |
WriteContentTo | Overridden. Saves all the children of the XmlDocument node to the specified XmlWriter. |
WriteTo | Overridden. Saves the XmlDocument node to the specified XmlWriter. |
Table 18. |
Protected Methods | |
Name | Description |
Symbol | |
CreateDefaultAttribute | Creates a default attribute with the specified prefix, local name and namespace URI. |
CreateNavigator | Overloaded. Creates a new XPathNavigator object for navigating this document. |
Finalize | Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
Table 19. |
Public Events | |
Name | Description |
Symbol | |
NodeChanged | Occurs when the Value of a node belonging to this document has been changed. |
NodeChanging | Occurs when the Value of a node belonging to this document is about to be changed. |
NodeInserted | Occurs when a node belonging to this document has been inserted into another node. |
NodeInserting | Occurs when a node belonging to this document is about to be inserted into another node. |
NodeRemoved | Occurs when a node belonging to this document has been removed from its parent. |
NodeRemoving | Occurs when a node belonging to this document is about to be removed from the document. |
Table 20. |
You’ll notice a lot of references to nodes in the preceding tables. The DOM tree that an XmlDocument object builds in memory is composed of nodes, each of which is an object of a class that inherits from the abstract XmlNode base class. Just about everything in an XML document is represented by a node. For example:
Elements are represented by the XmlElement class.
Attributes are represented by the XmlAttribute class.
The text content of elements is represented by the XmlText class.
Comments are represented by the XmlComment class.
The XmlNode class provides common functionality for all these node types. Because this functionality is so important when working with XmlDocument, We’ve listed the properties and methods of XmlNode in the following two tables.
Public Properties | |
Name | Description |
Symbol | |
Attributes | Gets an XmlAttributeCollection containing the attributes of this node. |
BaseURI | Gets the base URI of the current node. |
ChildNodes | Gets all the child nodes of the node. |
FirstChild | Gets the first child of the node. |
HasChildNodes | Gets a value indicating whether this node has any child nodes. |
InnerText | Gets or sets the concatenated values of the node and all its child nodes. |
InnerXml | Gets or sets the markup representing only the child nodes of this node. |
IsReadOnly | Gets a value indicating whether the node is read-only. |
Item | Overloaded. Gets the specified child element. |
LastChild | Gets the last child of the node. |
LocalName | When overridden in a derived class, gets the local name of the node. |
Name | When overridden in a derived class, gets the qualified name of the node. |
NamespaceURI | Gets the namespace URI of this node. |
NextSibling | Gets the node immediately following this node. |
NodeType | When overridden in a derived class, gets the type of the current node. |
OuterXml | Gets the markup representing this node and all its child nodes. |
OwnerDocument | Gets the XmlDocument to which this node belongs. |
ParentNode | Gets the parent of this node (for nodes that can have parents). |
Prefix | Gets or sets the namespace prefix of this node. |
PreviousSibling | Gets the node immediately preceding this node. |
SchemaInfo | Gets the post schema validation infoset that has been assigned to this node as a result of schema validation. |
Value | Gets or sets the value of the node. |
Table 21. |
Public Methods | |
Name | Description |
Symbol | |
AppendChild | Adds the specified node to the end of the list of child nodes, of this node. |
Clone | Creates a duplicate of this node. |
CloneNode | When overridden in a derived class, creates a duplicate of the node. |
CreateNavigator | Creates an XPathNavigator for navigating this object. |
Equals | Overloaded. Determines whether two Object instances are equal. (Inherited from Object.) |
GetEnumerator | Provides support for the for each style iteration over the nodes in the XmlNode. |
GetHashCode | Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.) |
GetNamespaceOfPrefix | Looks up the closest xmlns declaration for the given prefix that is in scope for the current node and returns the namespace URI in the declaration. |
GetPrefixOfNamespace | Looks up the closest xmlns declaration for the given namespace URI that is in scope for the current node and returns the prefix defined in that declaration. |
GetType | Gets the Type of the current instance. (Inherited from Object.) |
InsertAfter | Inserts the specified node immediately after the specified reference node. |
InsertBefore | Inserts the specified node immediately before the specified reference node. |
Normalize | Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes. |
PrependChild | Adds the specified node to the beginning of the list of child nodes for this node. |
ReferenceEquals | Determines whether the specified Object instances are the same instance. (Inherited from Object.) |
RemoveAll | Removes all the child nodes and/or attributes of the current node. |
RemoveChild | Removes specified child node. |
ReplaceChild | Replaces the child node oldChild with newChild node. |
SelectNodes | Overloaded. Selects a list of nodes matching the XPath expression. |
SelectSingleNode | Overloaded. Selects the first XmlNode that matches the XPath expression. |
Supports | Test if the DOM implementation implements a specific feature. |
ToString | Returns a String that represents the current Object. (Inherited from Object.) |
WriteContentTo | When overridden in a derived class, saves all the child nodes of the node to the specified XmlWriter. |
WriteTo | When overridden in a derived class, saves the current node to the specified XmlWriter. |
Table 22. |
Protected Methods | |
Name | Description |
Symbol | |
Finalize | Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
Table 23. |
Explicit Interface Implementations | |
Name | Description |
Symbol | |
System.Collections.IEnumerable.GetEnumerator | For a description of this member, see XmlNode.GetEnumerator. |
System.ICloneable.Clone | For a description of this member, see XmlNode.Clone. |
Table 24. |
Perhaps the most important descendant of XmlNode is XmlElement, which represents an element within a document. This class adds a number of methods to XmlNode, most of which are concerned with getting, setting, and removing attributes.