I was able to create a Word Document with content controls mapped to an Xml schema and using the code from this blog: http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/ I am able to insert data into the word document.
The question I have is, is it possible to replace the the code below so that I can use an Xml file instead of having to write this for each finding:
//create XML string matching schema custom XML path
string newXml = "<root>" +
"<FINDING>Adobe Flash Player contains multiple...</FINDING>" +
"<STATUS>Open</STATUS>" +
"<THREATLEVEL>High</THREATLEVEL>" +
"<RECOMMENDATION>Update Flash Player to version...</RECOMMENDATION>" +
"<DEVICEAFFECTED>UserPC</DEVICEAFFECTED>" +
"<SCANNER>XXXXXX</SCANNER>" +
"</root>";
I have tried replacing this with:
string newXml = @”C:\Users\Christopher\Desktop\BookData\TestReport.xml”;
and created a nested using statement with StreamReader and the existing StreamWriter but the word document would not populate and there would not be any errors.
–I just tried to replace that code with this:
//create XML string matching schema custom XML path
string newXml = @”C:\Users\Christopher\Desktop\BookData\TestReport.xml”;
using (StreamReader sr = new StreamReader (newXml))
{
newXml = sr.ReadToEnd();
}
and I no longer get the error when I open the document, but the content controls are not populating?
Thank you.
Turns out the problem I was running into was that my code and examples were not actually deleting the previous CustomXMLParts located in the “CustomXML” folder. I the code working in two ways, the first is a Windows Form App with one button (btnGenerate) which allows you to select both the template.docx and customXML.xml files. The second is the a Console Program.
Cheers
Windows Form Application:
Here is the Console Program Version
I hope you fellow developers and office automation folks out there are able put this to good use!