XmlDocument.InnerXml Property

Definition

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

public override string InnerXml { get; set; }

Property Value

The markup of the children of the current node.

Exceptions

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

Remarks

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"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="stock">
         ...
     </xsl:template>
    </xsl:stylesheet>

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

<xsl:template match="stock"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     ...
    </xsl:template>

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 declaration, 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:

<test>
      <item>123</item>
    </test>

InnerXml returns a plain XML string with no namespace declarations:

<item>123</item>

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

<test2 xmlns="urn:1">
    </test>

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">
      <item>123</item>
    </test>

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

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0