In one of the demos I am doing for the Dutch Office Developer Conference I show what is required for a Word document to have some text, a hyperlink and an image using the bare minimum of xml markup. I've expanded the sample with a custom xml part mapped to a content control. For more information on using custom Xml, head over to Brian Jones' blog.
The custom XML part looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mailing xmlns="http://contoso.com/2005/mailings">
<recipient>
<name>Test</name>
</recipient>
</mailing>
To map this custom xml part to your document using notepad (ok,ok, you can use Visual Studio), first download the starter file, and then take the following steps:
Step 1: Verify content types
Make sure the ‘xml’ extension is mapped to the ‘application/xml’ content type. You can check this in the ‘[Content_types].xml’ file in the root of the zip container. There should also be an Override content type which maps ‘document.xml’ to the main office document type.
Step 2: Create the custom XML part
Add a file called ‘item1.xml’ to the ‘customXML’ folder. The contents for this file is shown at the top of this article.
Step 3: Relate the custom XML part
We’ll need to edit the relationship file for the main document part (document.xml.rels). Add a reference to the custom XML part which sits in the ‘customXML’ folder. The relationship's Type attribute is important and should be of the ‘customXML’ type.
Add the following markup to the 'document.xml.rels' file.
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"
Target="../customXML/item1.xml" />
Make sure you don’t mess up the ‘target’ property. It is a relative path which is relative to the main document part’s url.
Step 4: Create the custom XML part properties
We now need to add a properties file which sits in the same directory as ‘item1.xml’, call this file ‘itemProps1.xml’. The property file tells Word about the xml namespaces used in your custom xml part. Also you can give your custom xml part an ID, which we’ll use to map the content control to the xml part.
The contents for ‘itemProps1.xml’ is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ds:datastoreItem ds:itemID="{C65DD089-1234-4A84-8443-BC4CB07DEB45}"
xmlns:ds="http://schemas.openxmlformats.org/officedocument/2006/2/customXml">
<ds:schemaRefs>
<ds:schemaRef ds:uri="http://contoso.com/2005/mailings"/>
</ds:schemaRefs>
</ds:datastoreItem>
Step 5: Relate the property file to the custom XML part
We need to create the relationship between ‘item1.xml’ and ‘itemProps1.xml’, in the appropriate folder. Since the relation originates from ‘item1.xml’, we need to use a ‘_rels’ subdirectory from the directory we find ‘item1.xml’ in. Call the relationship file ‘item1.xml.rels’.
This 'item1.xml.rels' file has the following contents:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps"
Target="itemProps1.xml"/>
</Relationships>
This relationship’s type attribute is different from the previous relation we created. It now points to an item of the ‘customXmlProps’ type.
Step 6: Create a mapped content control
This is the best step, update the main document part to add a new mapped content control. Open up ‘document.xml’ found in the ‘word’ folder and add the following content right before the closing </body> tag. The databinding element maps to your custom XML part using the ID you entered in the custom properties file, the ‘text’ node specifies a normal text content control.
Add the following markup to the main 'document.xml' file:
<w:p>
<w:r>
<w:sdt>
<w:sdtPr>
<w:dataBinding
w:prefixMappings="xmlns:ns0='http://contoso.com/2005/mailings'"
w:xpath="/ns0:mailing[1]/ns0:recipient[1]/ns0:name"
w:storeItemID="{C65DD089-1234-4A84-8443-BC4CB07DEB45}" />
<w:text />
</w:sdtPr>
</w:sdt>
</w:r>
</w:p>
That’s it, you’re done. You can also download the final file to see how it looks when finished.