当我从头开始构建XML时XmlDocument
,该OuterXml
属性已经通过换行符很好地缩进了所有内容.但是,如果我调用LoadXml
一些非常"压缩"的XML(没有换行或缩进),那么输出会OuterXml
保持这种状态.所以......
从一个实例中获取美化XML输出的最简单方法是XmlDocument
什么?
我遇到了一个奇怪的情况,希望有一个比我了解得更多的人可以帮助我解决这个问题。
我正在将图像插入Xml文档中,以便可以使用Microsoft Word打开它。作为其中的一部分,我需要添加一个映射到包含图像的元素的Xml'Relationship'。直截了当。
我添加的节点应如下所示:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" />
Run Code Online (Sandbox Code Playgroud)
但是,在最终的.doc文件中,该行显示如下:
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png" xmlns="" />
Run Code Online (Sandbox Code Playgroud)
即它现在具有一个空的xmlns =“”属性。
这足以让Word相信文档已损坏并拒绝打开。如果我手动打开文件并删除该属性,则会打开文件。
显然,我想以编程方式删除它:-)所以我找到了父节点。这是我的理解有些模糊的地方。我相信OuterXml元素包含节点及其所有子元素的内容,而InnerXml仅包含子元素。
这就是我所看到的(请注意,转义字符是因为我是从Visual Studio的文本查看器中剪切下来的)。
外层Xml:
"<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>"
Run Code Online (Sandbox Code Playgroud)
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\" …
Run Code Online (Sandbox Code Playgroud)