XmlDocument Class
TOC
Collapse the table of content
Expand the table of content

XmlDocument Class

 

Represents an XML document.

You can use this class to load, validate, edit, add, and position XML in a document. For examples, see the Tasks section on this page.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)


type XmlDocument = 
    class
        inherit XmlNode
    end

NameDescription
System_CAPS_pubmethodXmlDocument()

Initializes a new instance of the XmlDocument class.

System_CAPS_protmethodXmlDocument(XmlImplementation)

Initializes a new instance of the XmlDocument class with the specified XmlImplementation.

System_CAPS_pubmethodXmlDocument(XmlNameTable)

Initializes a new instance of the XmlDocument class with the specified XmlNameTable.

NameDescription
System_CAPS_pubpropertyAttributes

Gets an XmlAttributeCollection containing the attributes of this node.(Inherited from XmlNode.)

System_CAPS_pubpropertyBaseURI

Gets the base URI of the current node.(Overrides XmlNode.BaseURI.)

System_CAPS_pubpropertyChildNodes

Gets all the child nodes of the node.(Inherited from XmlNode.)

System_CAPS_pubpropertyDocumentElement

Gets the root XmlElement for the document.

System_CAPS_pubpropertyDocumentType

Gets the node containing the DOCTYPE declaration.

System_CAPS_pubpropertyFirstChild

Gets the first child of the node.(Inherited from XmlNode.)

System_CAPS_pubpropertyHasChildNodes

Gets a value indicating whether this node has any child nodes.(Inherited from XmlNode.)

System_CAPS_pubpropertyImplementation

Gets the XmlImplementation object for the current document.

System_CAPS_pubpropertyInnerText

Throws an InvalidOperationException in all cases.(Overrides XmlNode.InnerText.)

System_CAPS_pubpropertyInnerXml

Gets or sets the markup representing the children of the current node.(Overrides XmlNode.InnerXml.)

System_CAPS_pubpropertyIsReadOnly

Gets a value indicating whether the current node is read-only.(Overrides XmlNode.IsReadOnly.)

System_CAPS_pubpropertyItem(String)

Gets the first child element with the specified Name.(Inherited from XmlNode.)

System_CAPS_pubpropertyItem(String, String)

Gets the first child element with the specified LocalName and NamespaceURI.(Inherited from XmlNode.)

System_CAPS_pubpropertyLastChild

Gets the last child of the node.(Inherited from XmlNode.)

System_CAPS_pubpropertyLocalName

Gets the local name of the node.(Overrides XmlNode.LocalName.)

System_CAPS_pubpropertyName

Gets the qualified name of the node.(Overrides XmlNode.Name.)

System_CAPS_pubpropertyNamespaceURI

Gets the namespace URI of this node.(Inherited from XmlNode.)

System_CAPS_pubpropertyNameTable

Gets the XmlNameTable associated with this implementation.

System_CAPS_pubpropertyNextSibling

Gets the node immediately following this node.(Inherited from XmlNode.)

System_CAPS_pubpropertyNodeType

Gets the type of the current node.(Overrides XmlNode.NodeType.)

System_CAPS_pubpropertyOuterXml

Gets the markup containing this node and all its child nodes.(Inherited from XmlNode.)

System_CAPS_pubpropertyOwnerDocument

Gets the XmlDocument to which the current node belongs.(Overrides XmlNode.OwnerDocument.)

System_CAPS_pubpropertyParentNode

Gets the parent node of this node (for nodes that can have parents).(Overrides XmlNode.ParentNode.)

System_CAPS_pubpropertyPrefix

Gets or sets the namespace prefix of this node.(Inherited from XmlNode.)

System_CAPS_pubpropertyPreserveWhitespace

Gets or sets a value indicating whether to preserve white space in element content.

System_CAPS_pubpropertyPreviousSibling

Gets the node immediately preceding this node.(Inherited from XmlNode.)

System_CAPS_pubpropertyPreviousText

Gets the text node that immediately precedes this node.(Inherited from XmlNode.)

System_CAPS_pubpropertySchemaInfo

Returns the Post-Schema-Validation-Infoset (PSVI) of the node.(Overrides XmlNode.SchemaInfo.)

System_CAPS_pubpropertySchemas

Gets or sets the XmlSchemaSet object associated with this XmlDocument.

System_CAPS_pubpropertyValue

Gets or sets the value of the node.(Inherited from XmlNode.)

System_CAPS_pubpropertyXmlResolver

Sets the XmlResolver to use for resolving external resources.

NameDescription
System_CAPS_pubmethodAppendChild(XmlNode)

Adds the specified node to the end of the list of child nodes, of this node.(Inherited from XmlNode.)

System_CAPS_pubmethodClone()

Creates a duplicate of this node.(Inherited from XmlNode.)

System_CAPS_pubmethodCloneNode(Boolean)

Creates a duplicate of this node.(Overrides XmlNode.CloneNode(Boolean).)

System_CAPS_pubmethodCreateAttribute(String)

Creates an XmlAttribute with the specified Name.

System_CAPS_pubmethodCreateAttribute(String, String)

Creates an XmlAttribute with the specified qualified name and NamespaceURI.

System_CAPS_pubmethodCreateAttribute(String, String, String)

Creates an XmlAttribute with the specified Prefix, LocalName, and NamespaceURI.

System_CAPS_pubmethodCreateCDataSection(String)

Creates an XmlCDataSection containing the specified data.

System_CAPS_pubmethodCreateComment(String)

Creates an XmlComment containing the specified data.

System_CAPS_protmethodCreateDefaultAttribute(String, String, String)

Creates a default attribute with the specified prefix, local name and namespace URI.

System_CAPS_pubmethodCreateDocumentFragment()
System_CAPS_pubmethodCreateDocumentType(String, String, String, String)

Returns a new XmlDocumentType object.

System_CAPS_pubmethodCreateElement(String)

Creates an element with the specified name.

System_CAPS_pubmethodCreateElement(String, String)

Creates an XmlElement with the qualified name and NamespaceURI.

System_CAPS_pubmethodCreateElement(String, String, String)

Creates an element with the specified Prefix, LocalName, and NamespaceURI.

System_CAPS_pubmethodCreateEntityReference(String)

Creates an XmlEntityReference with the specified name.

System_CAPS_pubmethodCreateNavigator()

Creates a new XPathNavigator object for navigating this document.(Overrides XmlNode.CreateNavigator().)

System_CAPS_protmethodCreateNavigator(XmlNode)

Creates an XPathNavigator object for navigating this document positioned on the XmlNode specified.

System_CAPS_pubmethodCreateNode(String, String, String)

Creates an XmlNode with the specified node type, Name, and NamespaceURI.

System_CAPS_pubmethodCreateNode(XmlNodeType, String, String)

Creates an XmlNode with the specified XmlNodeType, Name, and NamespaceURI.

System_CAPS_pubmethodCreateNode(XmlNodeType, String, String, String)

Creates a XmlNode with the specified XmlNodeType, Prefix, Name, and NamespaceURI.

System_CAPS_pubmethodCreateProcessingInstruction(String, String)

Creates an XmlProcessingInstruction with the specified name and data.

System_CAPS_pubmethodCreateSignificantWhitespace(String)

Creates an XmlSignificantWhitespace node.

System_CAPS_pubmethodCreateTextNode(String)

Creates an XmlText with the specified text.

System_CAPS_pubmethodCreateWhitespace(String)

Creates an XmlWhitespace node.

System_CAPS_pubmethodCreateXmlDeclaration(String, String, String)

Creates an XmlDeclaration node with the specified values.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetElementById(String)

Gets the XmlElement with the specified ID.

System_CAPS_pubmethodGetElementsByTagName(String)

Returns an XmlNodeList containing a list of all descendant elements that match the specified Name.

System_CAPS_pubmethodGetElementsByTagName(String, String)

Returns an XmlNodeList containing a list of all descendant elements that match the specified LocalName and NamespaceURI.

System_CAPS_pubmethodGetEnumerator()

Get an enumerator that iterates through the child nodes in the current node.(Inherited from XmlNode.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetNamespaceOfPrefix(String)

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.)

System_CAPS_pubmethodGetPrefixOfNamespace(String)

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.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodImportNode(XmlNode, Boolean)

Imports a node from another document to the current document.

System_CAPS_pubmethodInsertAfter(XmlNode, XmlNode)

Inserts the specified node immediately after the specified reference node.(Inherited from XmlNode.)

System_CAPS_pubmethodInsertBefore(XmlNode, XmlNode)

Inserts the specified node immediately before the specified reference node.(Inherited from XmlNode.)

System_CAPS_pubmethodLoad(Stream)

Loads the XML document from the specified stream.

System_CAPS_pubmethodLoad(String)

Loads the XML document from the specified URL.

System_CAPS_pubmethodLoad(TextReader)

Loads the XML document from the specified TextReader.

System_CAPS_pubmethodLoad(XmlReader)

Loads the XML document from the specified XmlReader.

System_CAPS_pubmethodLoadXml(String)

Loads the XML document from the specified string.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodNormalize()

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.)

System_CAPS_pubmethodPrependChild(XmlNode)

Adds the specified node to the beginning of the list of child nodes for this node.(Inherited from XmlNode.)

System_CAPS_pubmethodReadNode(XmlReader)

Creates an XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or attribute.

System_CAPS_pubmethodRemoveAll()

Removes all the child nodes and/or attributes of the current node.(Inherited from XmlNode.)

System_CAPS_pubmethodRemoveChild(XmlNode)

Removes specified child node.(Inherited from XmlNode.)

System_CAPS_pubmethodReplaceChild(XmlNode, XmlNode)

Replaces the child node oldChild with newChild node.(Inherited from XmlNode.)

System_CAPS_pubmethodSave(Stream)

Saves the XML document to the specified stream.

System_CAPS_pubmethodSave(String)

Saves the XML document to the specified file. If the specified file exists, this method overwrites it.

System_CAPS_pubmethodSave(TextWriter)

Saves the XML document to the specified TextWriter.

System_CAPS_pubmethodSave(XmlWriter)

Saves the XML document to the specified XmlWriter.

System_CAPS_pubmethodSelectNodes(String)

Selects a list of nodes matching the XPath expression.(Inherited from XmlNode.)

System_CAPS_pubmethodSelectNodes(String, XmlNamespaceManager)

Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.(Inherited from XmlNode.)

System_CAPS_pubmethodSelectSingleNode(String)

Selects the first XmlNode that matches the XPath expression.(Inherited from XmlNode.)

System_CAPS_pubmethodSelectSingleNode(String, XmlNamespaceManager)

Selects the first XmlNode that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied XmlNamespaceManager.(Inherited from XmlNode.)

System_CAPS_pubmethodSupports(String, String)

Tests if the DOM implementation implements a specific feature.(Inherited from XmlNode.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodValidate(ValidationEventHandler)

Validates the XmlDocument against the XML Schema Definition Language (XSD) schemas contained in the Schemas property.

System_CAPS_pubmethodValidate(ValidationEventHandler, XmlNode)

Validates the XmlNode object specified against the XML Schema Definition Language (XSD) schemas in the Schemas property.

System_CAPS_pubmethodWriteContentTo(XmlWriter)

Saves all the children of the XmlDocument node to the specified XmlWriter.(Overrides XmlNode.WriteContentTo(XmlWriter).)

System_CAPS_pubmethodWriteTo(XmlWriter)

Saves the XmlDocument node to the specified XmlWriter.(Overrides XmlNode.WriteTo(XmlWriter).)

NameDescription
System_CAPS_pubeventNodeChanged

Occurs when the Value of a node belonging to this document has been changed.

System_CAPS_pubeventNodeChanging

Occurs when the Value of a node belonging to this document is about to be changed.

System_CAPS_pubeventNodeInserted

Occurs when a node belonging to this document has been inserted into another node.

System_CAPS_pubeventNodeInserting

Occurs when a node belonging to this document is about to be inserted into another node.

System_CAPS_pubeventNodeRemoved

Occurs when a node belonging to this document has been removed from its parent.

System_CAPS_pubeventNodeRemoving

Occurs when a node belonging to this document is about to be removed from the document.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

For a description of this member, see XmlNode.GetEnumerator.(Inherited from XmlNode.)

System_CAPS_pubinterfaceSystem_CAPS_privmethodICloneable.Clone()

For a description of this member, see XmlNode.Clone.(Inherited from XmlNode.)

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<'TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<'TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)


The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2.

DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).

You can load XML into the DOM by using the XmlDocument class, and then programmatically read, modify, and remove XML in the document.

If you want to pry open the XmlDocument class and see how it’s implemented, see the Reference Source.

Start with an XML document like this one. It’s just a few books in a collection. But it does contain the basic things that you’d find in any XML document; a namespace, elements that represent data and attributes that describe the data.

<?xml version="1.0" encoding="utf-8"?>
<books xmlns="http://www.contoso.com/books">
  <book genre="novel" ISBN="1-861001-57-8" publicationdate="1823-01-28">
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
  <book genre="novel" ISBN="1-861002-30-1" publicationdate="1985-01-01">
    <title>The Handmaid's Tale</title>
    <price>29.95</price>
  </book>
  <book genre="novel" ISBN="1-861001-45-3" publicationdate="1811-01-01">
    <title>Sense and Sensibility</title>
    <price>19.95</price>
  </book>
</books>

Next, load this data into the DOM so that you can work with it in memory. The most popular way to do this is refer to a file on your local computer or on a network.

This example loads XML from a file. If the file doesn’t exist, it just generates some XML and loads that.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Learn more:Reading an XML Document into the DOM

Start with an XML schema like this one. This schema defines the data types in the XML and which attributes are required.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  attributeFormDefault="unqualified" 
  elementFormDefault="qualified"
  targetNamespace="http://www.contoso.com/books"> 
  <xs:element name="books">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="book">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="price" type="xs:decimal" />
            </xs:sequence>
            <xs:attribute name="genre" type="xs:string" use="required" />
            <xs:attribute name="ISBN" type="xs:string" use="required" />
            <xs:attribute name="publicationdate" type="xs:date" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Create an XmlReader object by using your schema, and then load that object into the DOM. Create an event handler that executes when code attempts to modify your XML file in ways that violate the rules of the schema.

These blocks of code show helper methods that do all of this.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Learn more:Validating an XML Document in the DOM

You can use properties to navigate around an XML document. But before you use any of them, let’s quickly review a few terms. Your document is composed of nodes. Each node has as single parent node directly above it. The only node that does not have a parent node is the document root, as it is the top-level node. Most nodes can have child nodes, which are nodes directly below them. Nodes that are at the same level are siblings.

The following examples, show you how to obtain the root node, jump to the first child node of the root node, access any of its child nodes, get back out to the parent node, and then navigate across sibling nodes.

Start with the root node

This example gets the root node and then uses that node to output the contents of the document to the console.

No code example is currently available or this language may not be supported.

Get child nodes

This example jumps to the first child node of the root node and then iterates through the child nodes of that node if any exist.

No code example is currently available or this language may not be supported.

Get back to the parent node

Use the ParentNode property.

Refer to the last child node

This example writes the price of a book to the console (which is the last child node of a book node).

No code example is currently available or this language may not be supported.

Navigate forward across siblings

This example moves forward from book to book. Book nodes are siblings to one another.

No code example is currently available or this language may not be supported.

Navigate backwards across siblings

This example moves backwards from book to book.

No code example is currently available or this language may not be supported.

The most popular way to find one or more nodes of data is to use an XPath query string, but there are also methods that don’t require one.

Get a single node

This example locates a book by using the ISBN number.

No code example is currently available or this language may not be supported.

The string used in this example is an Xpath query. You can find more examples of them here: XPath examples.

You can also use the GetElementById to retrieve nodes. To use this approach, you’ll have to define ID’s in the document type definition declarations of your XML file.

After you get a node, you get the value of attributes or child nodes. This example does that with a book node.

No code example is currently available or this language may not be supported.

Get a collection of nodes

This example selects all books where the author's last name is Austen, and then changes the price of those books.

No code example is currently available or this language may not be supported.

You can also get a collection of nodes by using the name of the node. For example, this example gets a collection of all book titles.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Learn more:Select Nodes Using XPath Navigation

This example edits a book node and its attributes.

No code example is currently available or this language may not be supported.

Learn more:Modifying Nodes, Content, and Values in an XML Document

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

To add a node, use the CreateElement method or the CreateNode method.

To add a data node such as a book, use the CreateElement method.

For any other type of node such as a comment, whitespace node, or CDATA node, use the CreateNode method.

This example creates a book node, adds attrubutes to that node, and then adds that node to the document.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Learn more:Inserting Nodes into an XML Document

To remove a node, use the RemoveChild method.

This example removes a book from the document and any whitespace that appears just before the book node.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Learn more:Removing Nodes, Content, and Values from an XML Document

You can choose where you want a node to appear in your document by using the InsertBefore and InsertAfter methods.

This example shows two helper methods. One of them moves a node higher in a list. The other one moves a node lower.

These methods could be used in an application that enables users to move books up and down in a list of books. When a user chooses a book and presses an up or down button, your code could call methods like these to position the corresponding book node before or after other book nodes.

No code example is currently available or this language may not be supported.

Complete sample:Manipulate XML in-Memory by using the XmlDocument class and other related types

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft