XmlDocument.InnerXml Property


Gets or sets the markup representing the children of the current node.

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

Public Overrides Property InnerXml As String

Property Value

Type: System.String

The markup of the children of the current node.

Exception Condition

The XML specified when setting this property is not well-formed.

Setting this property replaces the children of the node with the parsed contents of the given string. The parsing is done in the current namespace context.

InnerXml removes redundant namespace declarations. As a result, numerous cut and paste operations do not increase the size of your document with redundant namespace declarations. Consider the following XSL document:

<xsl:stylesheet version="1.0" 
     <xsl:template match="stock">

The InnerXml property on the stylesheet node returns the following string:

<xsl:template match="stock" 

Notice the additional xmlns:xsl namespace declaration which is used to preserve the node identity. If you re-insert this inner XML string, you will get back your original document. In other words, InnerXml recognizes that the xmlns:xsl namespace declaration is redundant, given that the parent xsl:stylesheet element already has the xmlns:xsl namespace delcaration, and therefore removes it.

If you move InnerXml from a document with no default namespace to a document with a default namespace, the behavior is a little different. Consider the following XML string:


InnerXml returns a plain XML string with no namespace declarations:


If you then insert this string into a document that does have a default namespace, such as the following:

<test2 xmlns="urn:1">

InnerXml parses the string in context, and the new nodes pick up the urn:1 namespace. The result looks like this:

<test2 xmlns="urn:1">

Now when you ask for the InnerXml you get back the following:

<item xmlns="urn:1">123</item>

If you explicitly want the inserted item to preserve the fact that it came from a document that had no namespace then you need to manually add an xmlns= "" declaration and insert the resulting string:

<item xmlns="">123</item>

The net effect of all this is as follows:

  1. Cutting and pasting InnerXml among documents that do not use namespaces is clean and simple and does not create "xmlns" redundancy in your strings.

  2. InnerXml can also be used to cut and paste between documents which have more than one namespace (that is XSL stylesheets).

  3. xmlns:xsl declarations automatically appear in your strings and disappear in your node hierarchies.

  4. In the case where you are moving InnerXml from a document that has no default namespace to a document that does have a default namespace, the new nodes will pick up the new default namespace.

If InnerXml is set with text containing entity references that are not currently defined in the document, the resulting tree will contain empty EntityReference nodes.

This property is a Microsoft extension to the Document Object Model (DOM).

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top