[英]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.