我遇到了一个奇怪的情况,希望能有比我更懂的人帮助我解决。
我正在将一张图片插入到 Xml 文档中,以便可以在 Microsoft Word 中打开。为此,我需要添加一个映射到包含该图片的元素的 Xml“关系”(Relationship)。很直观。
我正在添加一个节点,应该长这样:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" />
然而,在最终的.doc文件中,同一行会出现如下内容:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" xmlns="" />
即现在它具有一个空的xmlns=""属性。
这足以让Word认为文档已经损坏并拒绝打开。如果我手动打开文件并删除该属性,则文件会打开。
显然,我想以编程方式删除它 :-) 所以我找到了父节点。这是我理解有些模糊的地方。我认为OuterXml元素包含节点及其所有子项的内容,而InnerXml只包含子项。
以下是我看到的内容(请注意,转义字符是因为我从Visual Studio的文本查看器中剪切)。
OuterXml:
"<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">
<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\" />
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" />
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\" />
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme1.xml\" />
<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\" />
<Relationship Id=\"rId6\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/image1.png\" xmlns=\"\" />
</Relationships>"
InnerXml:
"<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings\" Target=\"webSettings.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId2\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId1\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\" Target=\"styles.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId5\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme\" Target=\"theme/theme1.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId4\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\" Target=\"fontTable.xml\" xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\" />
<Relationship Id=\"rId6\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/image1.png\" />"
请注意第六个也是最后一个元素,在OuterXml中有错误的xmlns="",但在InnerXml中没有。我可以轻松更改InnerXml,但无法更改OuterXml。
因此,我的最终问题是“如何摆脱这个添加的属性?”,但我也希望有人能解释一下内部和外部Xml之间的区别(除了容器)。