[英]Can't populate Word Content Control with custom XML
我正在尝试编写一些代码,以使用XML文件使用某些Content Control填充Word文件。 为此,我使用以下代码:
string outPath = @"D:\template_created.docx";
string docPath = @"D:\template.docx";
string xmlPath = @"D:\template.xml";
File.Copy(docPath, outPath);
using (WordprocessingDocument doc = WordprocessingDocument.Open(outPath, true))
{
MainDocumentPart mdp = doc.MainDocumentPart;
if (mdp.CustomXmlParts != null)
{
mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
}
CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
FileStream fs = null;
try
{
fs = new FileStream(xmlPath, FileMode.Open);
cxp.FeedData(fs);
mdp.Document.Save();
}
finally
{
if (fs != null)
{
fs.Dispose();
}
}
}
这是我的XML文件:
<root>
<firstname_1>Marc</firstname_1>
<lastname_1>Swit</lastname_1>
<firstname_2>Paul</firstname_2>
<lastname_2>Gevaert</lastname_2>
</root>
当我运行该应用程序时,它创建了自定义XML文件并将其附加到我的Word文件中。 当我打开Word文件时,没有错误,但是所有内容控件都没有填充
当我将template_created.docx重命名为template_created.zip,提取文件并进行浏览时,我发现我的XML文件正确创建,并且customXML文件夹中的内容正确。
有人有办法帮助我吗?
谢谢 !
编辑17/08/15:我在下面的答案中的第一个解决方案
我找到了解决问题的方法。 实际上,首先,我需要创建一个XML文件,其中包含与我的Contents Control对应的所有元素,然后在Word Content Control Toolkit中使用该文件将XML附加到我的Word文件之后
之后,当我运行我的应用程序时,将填充出价工作和内容控制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.