我遇到了一个奇怪的情况,我希望一个比我更了解的人能帮助我解决这个问题。
我正在将图像插入到Xml文档中,这样就可以用Microsoft打开它。作为其中的一部分,我需要添加一个Xml‘关系’,它映射到包含图像的元素。够直截了当的。
我正在添加应该如下所示的节点:
<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中的文本查看器中剪切出来的)。
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\" />"注意第6个也是最后一个元素是如何在OuterXml中有错误的OuterXml“”,而不是在InnerXml中。我可以轻松地更改InnerXml,但不能更改OuterXml。
因此,我的最终问题是“如何摆脱这个添加的属性?”,但我也希望有人能够解释为什么内部和外部的Xml之间存在差异(除了容器之外)。
发布于 2014-05-28 19:02:20
如何将节点添加到文档中?看起来发生这种情况是因为该元素没有命名空间(与其他名称空间为“http://schemas.openxmlformats.org/package/2006/relationships”的元素不同)。请记住,名称空间不像“普通”属性,对于标记的“标识”至关重要。
在"OuterXml“示例中,前5个关系节点都具有与父元素相同的名称空间,因此不需要显式定义它。第6个节点没有命名空间,因此xmlns="“
在"InnerXml“示例中,前5个节点都具有相同的命名空间,但是没有父节点可继承,因此每个节点都显式地定义了它。第6个节点仍然有空白的命名空间。
总之:文档不会因为字符串“xmlns=”而损坏,而是因为关系元素必须具有"http://schemas.openxmlformats.org/package/2006/relationships“的命名空间而损坏。
为了更好地说明,这里有一个示例xml文档。
<root xmlns="urn:foo:bar" xmlns:ns1="urn:baz">
<item />
<ns1:item />
<item xmlns="" />
</root>如果要获取根标记的“内部xml”,它可能如下所示:
<item xmlns="urn:foo:bar" />
<item xmlns="urn:baz" />
<item xmlns="" />如前所述,名称空间是标记的“标识”或任何您想要调用的名称的组成部分。下列文件在功能上都是相同的:
<foo:root xmlns:foo="urn:foo" xmlns:bar="urn:bar">
<foo:element />
<bar:element />
</foo:root>
<root xmlns="urn:foo" xmlns:bar="urn:bar">
<element />
<bar:element />
</root>
<root xmlns="urn:foo">
<element />
<element xmlns="urn:bar" />
</root>https://stackoverflow.com/questions/23919195
复制相似问题