6

I've come across a bizarre situation and I'm hoping that someone who understands better than I do can help me to resolve it.

I'm inserting an image into an Xml document such that it can be opened with Microsoft Word. As part of this, I need to add an Xml 'Relationship' which maps to the element containing the image. Straightforward enough.

I'm adding the node that should look like this:

<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"  />

However, in the final .doc file, the same line appears as this:

<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" xmlns="" />

i.e. it now has an empty xmlns="" attribute.

This is sufficient for Word to believe that the document is corrupt and refuse to open. If I manually open the file and delete that attribute, the file opens.

Clearly, I want to remove it programmatically :-) So I found the parent node. This is where my understanding is a little hazy. I believed that the OuterXml element contains the node & the contents of all it's children, while the InnerXml simply contains the children.

Here's what I'm seeing (note that the escape characters are because I've cut from the text viewer in 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\" />"

Note how the 6th and final element has the erroneous xmlns="" in the OuterXml, but not within the InnerXml. I can easily change the InnerXml, but not the OuterXml.

So, my ultimate question is "how do I get rid of this added attribute?", but I'm also hoping someone can explain why there is a difference between the Xml of the inner and outer (aside from the container).

4

1 回答 1

5

您如何将节点添加到文档中?看起来这种情况正在发生,因为该元素没有命名空间(与具有“ 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>
  • 根元素的命名空间是“urn:foo:bar”
  • 第一项元素的命名空间是“urn:foo:bar”
  • 第二个元素的命名空间是“urn:baz”
  • 第 3 个 item 元素的命名空间是 ""

如果您要获取根标签的“内部 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>
于 2014-05-28T19:02:20.213 回答