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