Monday, November 19, 2007

Using Custom XML islands together with Content Controls

To realize a link between Word 2007 Content Controls and Custom XML you can either do it with a tool such as Word 2007 content control toolkit or do it programmatically with XPath and add a dataBinding node for each Structured Document Tag(SDT) like this example:

w:dataBinding w:prefixMappings="" w:xpath="/myXml/dataRow/dataField" w:storeItemID="{CFD5333D-3117-4F96-8B72-CC1D814E0755}"

If you want to fill the custom xml dynamically - try the following:

using (Package p = Package.Open(generatedPath, FileMode.Open, FileAccess.ReadWrite))
// Now we can get the path to our custom XML in the template
Uri partUri = new Uri("/customXml/item1.xml", UriKind.Relative);
PackagePart part = p.GetPart(partUri);
// Overwrite existing part, therefore open with FileMode.Create
using (Stream s = part.GetStream(FileMode.Create))
// Now use the XmlSerialize to write back the content
XmlSerializer serializer = new XmlSerializer(typeof(Employee));
serializer.Serialize(s, employee);

// Flush memory-content back to the package

Just navigate to MzCool if you need an explanation of the whole context...

No comments: