Updating word document from xml data

Hello, I have a Word 07 document that has a few data fields that I have already mapped using the Word 07 Content Control Toolkit. What I want to do is be able to update my xml file with new info and have that change reflect in the Word document when I open it up. Currently, I have to update the xml data through the Content Control Toolkit. I have to save the file in the toolkit and then open it in Word to see my changes. How can I update the xml file through a text editor and have the changes show up in the corresponding fields in the Word document upon opening the document? Thank you for your help.
[627 byte] By [sumit381] at [2008-2-10]
# 1

First of all you should tag your customXML with a personal namespace in order to separate it from different customXML. This could look like:

<Books xmlns="http://contoso.com/2007/Books">

<Title></Title>

<Author></Author>

</Books>

It will help finding the right custom XML data store. Then you have to write some code that loops through all the existing data stores, loads the xml and compares the above mentioned namespace information. When found, you can assign your new XMLDocument (3. parameter of the function) and write it to the package using a StreamWriter.

private const string customBookSchema = http://contoso.com/2007/Books;

private void WriteCustomXML(string FilePath, string schema, XmlDocument xDocNewCustomXML)

{

using (WordprocessingDocument wDoc = WordprocessingDocument.Open(FilePath, true))

{

MainDocumentPart mainPart = wDoc.MainDocumentPart;

foreach (CustomXmlPart custXML in mainPart.CustomXmlParts)

{

XmlDocument xDocCustomXML = new XmlDocument();

xDocCustomXML.Load(custXML.GetStream(FileMode.Open));

XmlNode xn = xDocCustomXML.FirstChild;

if (!xn.HasChildNodes) xn = xn.NextSibling; // skip: <?xml version="1.0" encoding="utf-8" ?>

if (xn.NamespaceURI == schema) // is this the Part with the schema we are looking for?

{

xDocCustomXML = xDocNewCustomXML;

using (StreamWriter sw = new StreamWriter(custXML.GetStream(FileMode.Open, FileAccess.Write)))

{

xDocCustomXML.Save(sw);

}

}

}

}

}

Hope, this helps.

Jens

JensHa at 2007-10-2 > top of Msdn Tech,Office Live Development,Microsoft SDK for Open XML Formats...