Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class System.​Xml.​XmlDocument

Assembly: System.Xml.ReaderWriter

Inheritance: object → XmlNode → XmlDocument

Implemented Interfaces

Represents an XML document. You can use this class to load, validate, edit, add, and position XML in a document.

Properties

public string
BaseURI
Gets the base URI of the current node.
public XmlElement
DocumentElement
Gets the root <see cref="T:System.Xml.XmlElement" /> for the document.
DocumentType
Gets the node containing the DOCTYPE declaration.
Implementation
Gets the <see cref="T:System.Xml.XmlImplementation" /> object for the current document.
public string
InnerText
Throws an <see cref="T:System.InvalidOperationException" /> in all cases.
public string
InnerXml
Gets or sets the markup representing the children of the current node.
public bool
IsReadOnly
Gets a value indicating whether the current node is read-only.
public string
LocalName
Gets the local name of the node.
public string
Name
Gets the qualified name of the node.
public XmlNameTable
NameTable
Gets the <see cref="T:System.Xml.XmlNameTable" /> associated with this implementation.
public XmlNodeType
NodeType
Gets the type of the current node.
public XmlDocument
OwnerDocument
Gets the <see cref="T:System.Xml.XmlDocument" /> to which the current node belongs.
public XmlNode
ParentNode
Gets the parent node of this node (for nodes that can have parents).
public bool
PreserveWhitespace
Gets or sets a value indicating whether to preserve white space in element content.
SchemaInfo
Returns the Post-Schema-Validation-Infoset (PSVI) of the node.
public XmlSchemaSet
Schemas
Gets or sets the <see cref="T:System.Xml.Schema.XmlSchemaSet" /> object associated with this <see cref="T:System.Xml.XmlDocument" /> .
public XmlResolver
XmlResolver
Sets the <see cref="T:System.Xml.XmlResolver" /> to use for resolving external resources.
Attributes
Gets an <see cref="T:System.Xml.XmlAttributeCollection" /> containing the attributes of this node.
public XmlNodeList
ChildNodes
Gets all the child nodes of the node.
public XmlNode
FirstChild
Gets the first child of the node.
public bool
HasChildNodes
Gets a value indicating whether this node has any child nodes.
public string
InnerText
Gets or sets the concatenated values of the node and all its child nodes.
public XmlElement
Item
public XmlElement
Item
public XmlNode
LastChild
Gets the last child of the node.
public string
NamespaceURI
Gets the namespace URI of this node.
public XmlNode
NextSibling
Gets the node immediately following this node.
public string
OuterXml
Gets the markup containing this node and all its child nodes.
public string
Prefix
Gets or sets the namespace prefix of this node.
public XmlNode
PreviousSibling
Gets the node immediately preceding this node.
public XmlNode
PreviousText
Gets the text node that immediately precedes this node.
public string
Value
Gets or sets the value of the node.

Methods

public XmlNode
CloneNode​(bool deep)
Creates a duplicate of this node.
Returns The cloned <see langword="XmlDocument" /> node.
deep <see langword="true" /> to recursively clone the subtree under the specified node; <see langword="false" /> to clone only the node itself.
public XmlAttribute
CreateAttribute​(string name)
Creates an <see cref="T:System.Xml.XmlAttribute" /> with the specified <see cref="P:System.Xml.XmlDocument.Name" /> .
Returns The new <see langword="XmlAttribute" /> .
name The qualified name of the attribute. If the name contains a colon, the <see cref="P:System.Xml.XmlNode.Prefix" /> property reflects the part of the name preceding the first colon and the <see cref="P:System.Xml.XmlDocument.LocalName" /> property reflects the part of the name following the first colon. The <see cref="P:System.Xml.XmlNode.NamespaceURI" /> remains empty unless the prefix is a recognized built-in prefix such as xmlns. In this case <see langword="NamespaceURI" /> has a value of <c>http://www.w3.org/2000/xmlns/</c> .
public XmlAttribute
CreateAttribute​(string qualifiedName, string namespaceURI)
Creates an <see cref="T:System.Xml.XmlAttribute" /> with the specified qualified name and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlAttribute" /> .
qualifiedName The qualified name of the attribute. If the name contains a colon then the <see cref="P:System.Xml.XmlNode.Prefix" /> property will reflect the part of the name preceding the colon and the <see cref="P:System.Xml.XmlDocument.LocalName" /> property will reflect the part of the name after the colon.
namespaceURI The namespaceURI of the attribute. If the qualified name includes a prefix of xmlns, then this parameter must be <c>http://www.w3.org/2000/xmlns/</c> .
public XmlAttribute
CreateAttribute​(string prefix, string localName, string namespaceURI)
Creates an <see cref="T:System.Xml.XmlAttribute" /> with the specified <see cref="P:System.Xml.XmlNode.Prefix" /> , <see cref="P:System.Xml.XmlDocument.LocalName" /> , and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlAttribute" /> .
prefix The prefix of the attribute (if any). String.Empty and <see langword="null" /> are equivalent.
localName The local name of the attribute.
namespaceURI The namespace URI of the attribute (if any). String.Empty and <see langword="null" /> are equivalent. If <paramref name="prefix" /> is xmlns, then this parameter must be <c>http://www.w3.org/2000/xmlns/</c> ; otherwise an exception is thrown.
CreateCDataSection​(string data)
Creates an <see cref="T:System.Xml.XmlCDataSection" /> containing the specified data.
Returns The new <see langword="XmlCDataSection" /> .
data The content of the new <see langword="XmlCDataSection" /> .
public XmlComment
CreateComment​(string data)
Creates an <see cref="T:System.Xml.XmlComment" /> containing the specified data.
Returns The new <see langword="XmlComment" /> .
data The content of the new <see langword="XmlComment" /> .
CreateDocumentFragment​()
Creates an <see cref="T:System.Xml.XmlDocumentFragment" /> .
Returns The new <see langword="XmlDocumentFragment" /> .
CreateDocumentType​(string name, string publicId, string systemId, string internalSubset)
Returns a new <see cref="T:System.Xml.XmlDocumentType" /> object.
Returns The new <see langword="XmlDocumentType" /> .
name Name of the document type.
publicId The public identifier of the document type or <see langword="null" /> . You can specify a public URI and also a system identifier to identify the location of the external DTD subset.
systemId The system identifier of the document type or <see langword="null" /> . Specifies the URL of the file location for the external DTD subset.
internalSubset The DTD internal subset of the document type or <see langword="null" /> .
public XmlElement
CreateElement​(string name)
Creates an element with the specified name.
Returns The new <see langword="XmlElement" /> .
name The qualified name of the element. If the name contains a colon then the <see cref="P:System.Xml.XmlNode.Prefix" /> property reflects the part of the name preceding the colon and the <see cref="P:System.Xml.XmlDocument.LocalName" /> property reflects the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.
public XmlElement
CreateElement​(string qualifiedName, string namespaceURI)
Creates an <see cref="T:System.Xml.XmlElement" /> with the qualified name and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlElement" /> .
qualifiedName The qualified name of the element. If the name contains a colon then the <see cref="P:System.Xml.XmlNode.Prefix" /> property will reflect the part of the name preceding the colon and the <see cref="P:System.Xml.XmlDocument.LocalName" /> property will reflect the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.
namespaceURI The namespace URI of the element.
public XmlElement
CreateElement​(string prefix, string localName, string namespaceURI)
Creates an element with the specified <see cref="P:System.Xml.XmlNode.Prefix" /> , <see cref="P:System.Xml.XmlDocument.LocalName" /> , and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see cref="T:System.Xml.XmlElement" /> .
prefix The prefix of the new element (if any). String.Empty and <see langword="null" /> are equivalent.
localName The local name of the new element.
namespaceURI The namespace URI of the new element (if any). String.Empty and <see langword="null" /> are equivalent.
CreateEntityReference​(string name)
Creates an <see cref="T:System.Xml.XmlEntityReference" /> with the specified name.
Returns The new <see langword="XmlEntityReference" /> .
name The name of the entity reference.
CreateNavigator​()
Creates a new <see cref="T:System.Xml.XPath.XPathNavigator" /> object for navigating this document.
Returns An <see cref="T:System.Xml.XPath.XPathNavigator" /> object.
public XmlNode
CreateNode​(string nodeTypeString, string name, string namespaceURI)
Creates an <see cref="T:System.Xml.XmlNode" /> with the specified node type, <see cref="P:System.Xml.XmlDocument.Name" /> , and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlNode" /> .
nodeTypeString String version of the <see cref="T:System.Xml.XmlNodeType" /> of the new node. This parameter must be one of the values listed in the table below.
name The qualified name of the new node. If the name contains a colon, it is parsed into <see cref="P:System.Xml.XmlNode.Prefix" /> and <see cref="P:System.Xml.XmlDocument.LocalName" /> components.
namespaceURI The namespace URI of the new node.
public XmlNode
CreateNode​(XmlNodeType type, string name, string namespaceURI)
Creates an <see cref="T:System.Xml.XmlNode" /> with the specified <see cref="T:System.Xml.XmlNodeType" /> , <see cref="P:System.Xml.XmlDocument.Name" /> , and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlNode" /> .
type The <see langword="XmlNodeType" /> of the new node.
name The qualified name of the new node. If the name contains a colon then it is parsed into <see cref="P:System.Xml.XmlNode.Prefix" /> and <see cref="P:System.Xml.XmlDocument.LocalName" /> components.
namespaceURI The namespace URI of the new node.
public XmlNode
CreateNode​(XmlNodeType type, string prefix, string name, string namespaceURI)
Creates a <see cref="T:System.Xml.XmlNode" /> with the specified <see cref="T:System.Xml.XmlNodeType" /> , <see cref="P:System.Xml.XmlNode.Prefix" /> , <see cref="P:System.Xml.XmlDocument.Name" /> , and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns The new <see langword="XmlNode" /> .
type The <see langword="XmlNodeType" /> of the new node.
prefix The prefix of the new node.
name The local name of the new node.
namespaceURI The namespace URI of the new node.
CreateProcessingInstruction​(string target, string data)
Creates an <see cref="T:System.Xml.XmlProcessingInstruction" /> with the specified name and data.
Returns The new <see langword="XmlProcessingInstruction" /> .
target The name of the processing instruction.
data The data for the processing instruction.
CreateSignificantWhitespace​(string text)
Creates an <see cref="T:System.Xml.XmlSignificantWhitespace" /> node.
Returns A new <see langword="XmlSignificantWhitespace" /> node.
text The string must contain only the following characters &amp;#20; &amp;#10; &amp;#13; and &amp;#9;.
public XmlText
CreateTextNode​(string text)
Creates an <see cref="T:System.Xml.XmlText" /> with the specified text.
Returns The new <see langword="XmlText" /> node.
text The text for the Text node.
public XmlWhitespace
CreateWhitespace​(string text)
Creates an <see cref="T:System.Xml.XmlWhitespace" /> node.
Returns A new <see langword="XmlWhitespace" /> node.
text The string must contain only the following characters &amp;#20; &amp;#10; &amp;#13; and &amp;#9;.
CreateXmlDeclaration​(string version, string encoding, string standalone)
Creates an <see cref="T:System.Xml.XmlDeclaration" /> node with the specified values.
Returns The new <see langword="XmlDeclaration" /> node.
version The version must be "1.0".
encoding The value of the encoding attribute. This is the encoding that is used when you save the <see cref="T:System.Xml.XmlDocument" /> to a file or a stream; therefore, it must be set to a string supported by the <see cref="T:System.Text.Encoding" /> class, otherwise <see cref="M:System.Xml.XmlDocument.Save(System.String)" /> fails. If this is <see langword="null" /> or String.Empty, the <see langword="Save" /> method does not write an encoding attribute on the XML declaration and therefore the default encoding, UTF-8, is used. Note: If the <see langword="XmlDocument" /> is saved to either a <see cref="T:System.IO.TextWriter" /> or an <see cref="T:System.Xml.XmlTextWriter" /> , this encoding value is discarded. Instead, the encoding of the <see langword="TextWriter" /> or the <see langword="XmlTextWriter" /> is used. This ensures that the XML written out can be read back using the correct encoding.
standalone The value must be either "yes" or "no". If this is <see langword="null" /> or String.Empty, the <see langword="Save" /> method does not write a standalone attribute on the XML declaration.
public XmlElement
GetElementById​(string elementId)
Gets the <see cref="T:System.Xml.XmlElement" /> with the specified ID.
Returns The <see langword="XmlElement" /> with the matching ID or <see langword="null" /> if no matching element is found.
elementId The attribute ID to match.
public XmlNodeList
GetElementsByTagName​(string name)
Returns an <see cref="T:System.Xml.XmlNodeList" /> containing a list of all descendant elements that match the specified <see cref="P:System.Xml.XmlDocument.Name" /> .
Returns An <see cref="T:System.Xml.XmlNodeList" /> containing a list of all matching nodes. If no nodes match <paramref name="name" /> , the returned collection will be empty.
name The qualified name to match. It is matched against the <see langword="Name" /> property of the matching node. The special value "*" matches all tags.
public XmlNodeList
GetElementsByTagName​(string localName, string namespaceURI)
Returns an <see cref="T:System.Xml.XmlNodeList" /> containing a list of all descendant elements that match the specified <see cref="P:System.Xml.XmlDocument.LocalName" /> and <see cref="P:System.Xml.XmlNode.NamespaceURI" /> .
Returns An <see cref="T:System.Xml.XmlNodeList" /> containing a list of all matching nodes. If no nodes match the specified <paramref name="localName" /> and <paramref name="namespaceURI" /> , the returned collection will be empty.
localName The LocalName to match. The special value "*" matches all tags.
namespaceURI NamespaceURI to match.
public XmlNode
ImportNode​(XmlNode node, bool deep)
Imports a node from another document to the current document.
Returns The imported <see cref="T:System.Xml.XmlNode" /> .
node The node being imported.
deep <see langword="true" /> to perform a deep clone; otherwise, <see langword="false" /> .
public void
Load​(IO.​Stream inStream)
Loads the XML document from the specified stream.
inStream The stream containing the XML document to load.
public void
Load​(IO.​TextReader txtReader)
Loads the XML document from the specified <see cref="T:System.IO.TextReader" /> .
txtReader The <see langword="TextReader" /> used to feed the XML data into the document.
public void
Load​(string filename)
Loads the XML document from the specified URL.
filename URL for the file containing the XML document to load. The URL can be either a local file or an HTTP URL (a Web address).
public void
Load​(XmlReader reader)
Loads the XML document from the specified <see cref="T:System.Xml.XmlReader" /> .
reader The <see langword="XmlReader" /> used to feed the XML data into the document.
public void
LoadXml​(string xml)
Loads the XML document from the specified string.
xml String containing the XML document to load.
public XmlNode
ReadNode​(XmlReader reader)
Creates an <see cref="T:System.Xml.XmlNode" /> object based on the information in the <see cref="T:System.Xml.XmlReader" /> . The reader must be positioned on a node or attribute.
Returns The new <see langword="XmlNode" /> or <see langword="null" /> if no more nodes exist.
reader The XML source.
public void
Save​(IO.​Stream outStream)
Saves the XML document to the specified stream.
outStream The stream to which you want to save.
public void
Save​(IO.​TextWriter writer)
Saves the XML document to the specified <see cref="T:System.IO.TextWriter" /> .
writer The <see langword="TextWriter" /> to which you want to save.
public void
Save​(string filename)
Saves the XML document to the specified file. If the specified file exists, this method overwrites it.
filename The location of the file where you want to save the document.
public void
Save​(XmlWriter w)
Saves the XML document to the specified <see cref="T:System.Xml.XmlWriter" /> .
w The <see langword="XmlWriter" /> to which you want to save.
public void
Validate​(ValidationEventHandler validationEventHandler)
Validates the <see cref="T:System.Xml.XmlDocument" /> against the XML Schema Definition Language (XSD) schemas contained in the <see cref="P:System.Xml.XmlDocument.Schemas" /> property.
validationEventHandler The <see cref="T:System.Xml.Schema.ValidationEventHandler" /> object that receives information about schema validation warnings and errors.
public void
Validate​(ValidationEventHandler? validationEventHandler, XmlNode nodeToValidate)
Validates the <see cref="T:System.Xml.XmlNode" /> object specified against the XML Schema Definition Language (XSD) schemas in the <see cref="P:System.Xml.XmlDocument.Schemas" /> property.
validationEventHandler The <see cref="T:System.Xml.Schema.ValidationEventHandler" /> object that receives information about schema validation warnings and errors.
nodeToValidate The <see cref="T:System.Xml.XmlNode" /> object created from an <see cref="T:System.Xml.XmlDocument" /> to validate.
public void
WriteContentTo​(XmlWriter xw)
Saves all the children of the <see langword="XmlDocument" /> node to the specified <see cref="T:System.Xml.XmlWriter" /> .
xw The <see langword="XmlWriter" /> to which you want to save.
public void
WriteTo​(XmlWriter w)
Saves the <see langword="XmlDocument" /> node to the specified <see cref="T:System.Xml.XmlWriter" /> .
w The <see langword="XmlWriter" /> to which you want to save.
public XmlNode
AppendChild​(XmlNode newChild)
Inherited from XmlNode
Adds the specified node to the end of the list of child nodes, of this node.
Returns The node added.
newChild The node to add. All the contents of the node to be added are moved into the specified location.
public XmlNode
Clone​()
Inherited from XmlNode
Creates a duplicate of this node.
Returns The cloned node.
GetEnumerator​()
Inherited from XmlNode
Gets an enumerator that iterates through the child nodes in the current node.
Returns An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the child nodes in the current node.
public string
GetNamespaceOfPrefix​(string prefix)
Inherited from XmlNode
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.
Returns The namespace URI of the specified prefix.
prefix The prefix whose namespace URI you want to find.
public string
GetPrefixOfNamespace​(string namespaceURI)
Inherited from XmlNode
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.
Returns The prefix for the specified namespace URI.
namespaceURI The namespace URI whose prefix you want to find.
public XmlNode
InsertAfter​(XmlNode newChild, XmlNode refChild)
Inherited from XmlNode
Inserts the specified node immediately after the specified reference node.
Returns The node being inserted.
newChild The node to insert.
refChild The reference node. <paramref name="newChild" /> is placed after <paramref name="refChild" /> .
public XmlNode
InsertBefore​(XmlNode newChild, XmlNode refChild)
Inherited from XmlNode
Inserts the specified node immediately before the specified reference node.
Returns The node being inserted.
newChild The node to insert.
refChild The reference node. <paramref name="newChild" /> is placed before this node.
public void
Normalize​()
Inherited from XmlNode
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.
public XmlNode
PrependChild​(XmlNode newChild)
Inherited from XmlNode
Adds the specified node to the beginning of the list of child nodes for this node.
Returns The node added.
newChild The node to add. All the contents of the node to be added are moved into the specified location.
public void
RemoveAll​()
Inherited from XmlNode
Removes all the child nodes and/or attributes of the current node.
public XmlNode
RemoveChild​(XmlNode oldChild)
Inherited from XmlNode
Removes specified child node.
Returns The node removed.
oldChild The node being removed.
public XmlNode
ReplaceChild​(XmlNode newChild, XmlNode oldChild)
Inherited from XmlNode
Replaces the child node <paramref name="oldChild" /> with <paramref name="newChild" /> node.
Returns The node replaced.
newChild The new node to put in the child list.
oldChild The node being replaced in the list.
public XmlNodeList
SelectNodes​(string xpath)
Inherited from XmlNode
Selects a list of nodes matching the XPath expression.
Returns An <see cref="T:System.Xml.XmlNodeList" /> containing a collection of nodes matching the XPath query.
xpath The XPath expression.
public XmlNodeList
SelectNodes​(string xpath, XmlNamespaceManager nsmgr)
Inherited from XmlNode
Selects a list of nodes matching the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied <see cref="T:System.Xml.XmlNamespaceManager" /> .
Returns An <see cref="T:System.Xml.XmlNodeList" /> containing a collection of nodes matching the XPath query.
xpath The XPath expression. See XPath Examples.
nsmgr An <see cref="T:System.Xml.XmlNamespaceManager" /> to use for resolving namespaces for prefixes in the XPath expression.
public XmlNode
SelectSingleNode​(string xpath)
Inherited from XmlNode
Selects the first <see langword="XmlNode" /> that matches the XPath expression.
Returns The first <see langword="XmlNode" /> that matches the XPath query or <see langword="null" /> if no matching node is found.
xpath The XPath expression. See XPath Examples.
public XmlNode
SelectSingleNode​(string xpath, XmlNamespaceManager nsmgr)
Inherited from XmlNode
Selects the first <see langword="XmlNode" /> that matches the XPath expression. Any prefixes found in the XPath expression are resolved using the supplied <see cref="T:System.Xml.XmlNamespaceManager" /> .
Returns The first <see langword="XmlNode" /> that matches the XPath query or <see langword="null" /> if no matching node is found.
xpath The XPath expression. See XPath Examples.
nsmgr An <see cref="T:System.Xml.XmlNamespaceManager" /> to use for resolving namespaces for prefixes in the XPath expression.
public bool
Supports​(string feature, string version)
Inherited from XmlNode
Tests if the DOM implementation implements a specific feature.
Returns <see langword="true" /> if the feature is implemented in the specified version; otherwise, <see langword="false" /> . The following table describes the combinations that return <see langword="true" /> . <list type="table"> <listheader> <term> Feature</term> <description> Version</description> </listheader> <item> <term> XML</term> <description> 1.0</description> </item> <item> <term> XML</term> <description> 2.0</description> </item> </list>
feature The package name of the feature to test. This name is not case-sensitive.
version The version number of the package name to test. If the version is not specified (null), supporting any version of the feature causes the method to return true.
public bool
Equals​(object obj)
Inherited from object
protected void
Finalize​()
Inherited from object
public int
GetHashCode​()
Inherited from object
public Type
GetType​()
Inherited from object
protected object
MemberwiseClone​()
Inherited from object
public string
ToString​()
Inherited from object