我使用以下代码将xml(openxml)注入Word中的段落范围。问题是我收到一条错误消息,指出“无法将XML标记插入指定位置”
c#code:
try
{
string oxml = ""; // this contains the xml listed below
// get the first paragraph
Paragraph p = this.Paragraphs[1];
object missing = Type.Missing;
// insert openxml formatted xml into paragraph range
p.Range.InsertXML(oxml, ref missing); // causes the exception
}
catch (Exception ex)
{
//_logger.Error("OpenXml Injection", ex);
}
要注入的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:p>
<w:r>
<w:t xml:space="preserve">(a) Costs and Expenses</w:t>
</w:r>
<w:del w:id="33350" w:author="Exemplify">
<w:r>
<w:delText xml:space="preserve">. </w:delText>
</w:r>
</w:del>
<w:r>
<w:t xml:space="preserve"> The</w:t>
</w:r>
<w:commentRangeStart w:id="33351" />
<w:del w:id="33352" w:author="Exemplify">
<w:r>
<w:delText xml:space="preserve"> Borrower</w:delText>
</w:r>
</w:del>
<w:commentRangeEnd w:id="33351" />
<w:r>
<w:commentReference w:id="33351" />
</w:r>
<w:r>
<w:t xml:space="preserve"> shall pay (i) all reasonable out-of-pocket expenses incurred by the Administrative Agent and its Affiliates</w:t>
</w:r>
<w:del w:id="33353" w:author="Exemplify">
<w:r>
<w:delText xml:space="preserve">, </w:delText>
</w:r>
</w:del>
<w:ins w:id="33354" w:author="Exemplify">
<w:r>
<w:t xml:space="preserve"> (</w:t>
</w:r>
</w:ins>
<w:r>
<w:t xml:space="preserve">.</w:t>
</w:r>
</w:p>
</w:document>
</pkg:package>
答案 0 :(得分:9)
要使用InsertXML,XML必须是完整,有效的Word 2003 WordProcessingML文档,或者是平面OPC格式的完整,有效的Open Office XML文档。目前,您拥有的XML不是其中之一。
要成为有效的Flat OPC软件包,软件包必须包含Parts,并定义必要的关系部分。
要成为有效的Word 2003 XML文档,名称空间声明需要不同,文档元素必须是
<w:wordDocument>
并在里面你需要一个
<w:body>
元素。我怀疑你所拥有的一些元素对于那个XML词汇表是无效的(你可以自己检查一下),但是例如删除了一些元素和属性的以下XML应该注入OK:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
<w:p>
<w:r>
<w:t>(a) Costs and Expenses</w:t>
</w:r>
<w:r>
<w:t> The</w:t>
</w:r>
<w:r>
</w:r>
<w:r>
<w:t> shall pay (i) all reasonable out-of-pocket expenses incurred by the Administrative Agent and its Affiliates</w:t>
</w:r>
<w:r>
<w:t>.</w:t>
</w:r>
</w:p>
</w:body>
</w:wordDocument>
对于Word 2007及更高版本的XML,所有内容都需要位于包中的一部分内,并且包必须定义一些关系。在这种情况下,将执行以下操作(您只需要在XML中实际引用的命名空间定义):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml">
<pkg:xmlData>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id = "rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml" />
</Relationships>
</pkg:xmlData>
</pkg:part>
<pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">
<pkg:xmlData>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:r>
<w:t>(a) Costs and Expenses</w:t>
</w:r>
<w:del w:id="33350" w:author="Exemplify">
<w:r>
<w:delText>. </w:delText>
</w:r>
</w:del>
<w:r>
<w:t> The</w:t>
</w:r>
<w:commentRangeStart w:id="33351" />
<w:del w:id="33352" w:author="Exemplify">
<w:r>
<w:delText> Borrower</w:delText>
</w:r>
</w:del>
<w:commentRangeEnd w:id="33351" />
<w:r>
<w:t> shall pay (i) all reasonable out-of-pocket expenses incurred by the Administrative Agent and its Affiliates</w:t>
</w:r>
<w:del w:id="33353" w:author="Exemplify">
<w:r>
<w:delText>, </w:delText>
</w:r>
</w:del>
<w:ins w:id="33354" w:author="Exemplify">
<w:r>
<w:t> (</w:t>
</w:r>
</w:ins>
<w:r>
<w:t>.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>
</pkg:xmlData>
</pkg:part>
</pkg:package>
我省略了xml:space =“preserve”属性 - 你可能需要它们。我也省略了以下块
<w:r>
<w:commentReference w:id="33351" />
</w:r>
会导致失败。我猜它是因为它实际上引用了一个不存在的Id的评论。