XmlDocument.InnerXml 属性

定义

获取或设置表示当前节点的子级的标记。Gets or sets the markup representing the children of the current node.

public:
 virtual property System::String ^ InnerXml { System::String ^ get(); void set(System::String ^ value); };
public override string InnerXml { get; set; }
member this.InnerXml : string with get, set
Public Overrides Property InnerXml As String

属性值

当前节点的子级的标记。The markup of the children of the current node.

例外

设置此属性时指定的 XML 格式不正确。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 删除冗余的命名空间声明。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. 请考虑以下 XSL 文档: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>  

样式表节点上的 InnerXml 属性返回以下字符串: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>  

请注意用于保留节点标识的其他 xmlns: xsl 命名空间声明。Notice the additional xmlns:xsl namespace declaration which is used to preserve the node identity. 如果重新插入此内部 XML 字符串,将返回原始文档。If you re-insert this inner XML string, you will get back your original document. 换句话说,InnerXml 认识到 xmlns: xsl 命名空间声明是冗余的,因为父 xsl: stylesheet 元素已经具有 xmlns: xsl 命名空间声明,因此将其删除。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.

如果将 InnerXml 从没有默认命名空间的文档移动到带有默认命名空间的文档,则该行为将稍有不同。If you move InnerXml from a document with no default namespace to a document with a default namespace, the behavior is a little different. 请考虑以下 XML 字符串:Consider the following XML string:

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

InnerXml 返回无命名空间声明的纯 XML 字符串: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 在上下文中分析字符串,新节点将选取 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">  
      <item>123</item>  
    </test>  

现在,当你请求 InnerXml 时,你将获得以下内容:Now when you ask for the InnerXml you get back the following:

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

如果您明确希望插入的项保留它来自没有命名空间的文档,则需要手动添加 xmlns = "" 声明并插入生成的字符串: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. 在不使用命名空间的文档之间进行剪切和粘贴 InnerXml 简洁简单,并且不会在字符串中创建 "xmlns" 冗余。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 还可用于在具有多个命名空间(即 XSL 样式表)的文档之间剪切和粘贴。InnerXml can also be used to cut and paste between documents which have more than one namespace (that is XSL stylesheets).

  3. xmlns: xsl 声明自动显示在您的字符串中,并在您的节点层次结构中消失。xmlns:xsl declarations automatically appear in your strings and disappear in your node hierarchies.

  4. 如果要将 InnerXml 从没有默认命名空间的文档移动到没有默认命名空间的文档,新节点将选取新的默认命名空间。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.

如果 InnerXml 设置的文本包含文档中当前未定义的实体引用,则生成的树将包含空的 EntityReference 节点。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.

此属性是文档对象模型 (DOM) 的 Microsoft 扩展。This property is a Microsoft extension to the Document Object Model (DOM).

适用于