Welcome to Office Zealot Sign in | Join | Help

Julie's Office Dev Blog

Sorta like yoga... just less flexible.
Modifying The Contents of CustomXML Part

I've spent much of the day frustrated using System.IO.Packaging to do a simple thing, modify the contents of a customXML part.  It seemed like a simple thing to do really but I was misled and under the false assumption that one had to delete the part and recreate the part.  For whatever reason, I never figured out, everytime I deleted the customXML part, the relationship went with it and broke my bindings.  With confirmation that this shouldn't be happening from “the inside,” I went back to my original code from 6am this morning, simply overwriting the part that already existed.  Working with ever changing beta code... always fun:

So here's the code snippet for overwriting a customXML part (note that I based this on the assumption of only one customXML part -- you'd need to check for multiple parts):

string xmlDocRelType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";

string officeDocRelType = @“http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument;

 

PackagePart xmlPart = null;

PackagePart documentPart = null;

Uri documentUri = null;

Uri xmlUri = null;

//open the package

using (Package officePackage = Package.Open(docPath, FileMode.Open, FileAccess.ReadWrite))

{

//find the document part

foreach (PackageRelationship relationship in officePackage.GetRelationshipsByType(officeDocRelType))

{

//only one document part

documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

documentPart = officePackage.GetPart(documentUri);

break;

}

 

XmlDocument partDoc;

//find the custom XML Uri

foreach(PackageRelationship relationship in documentPart.GetRelationshipsByType(xmlDocRelType))

{

//TODO: add namespace check as will have more than one

//custom XML in solution

xmlUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

break;

}

//get the customXML part

xmlPart = officePackage.GetPart(xmlUri);

//modify the contents of the part

Stream outputStream = xmlPart.GetStream(FileMode.Create, FileAccess.ReadWrite);

StreamWriter ts = new StreamWriter(outputStream);

ts.Write(xmlDoc);

ts.Flush();

ts.Close();

//close our package up

officePackage.Close();

}

Posted: Tuesday, March 28, 2006 6:23 PM by jkremer

Comments

maarten said:

Hi Julie,

Great to see you are back blogging. I was one of your 'blog-readers' when you blogged in 2004 and was really surprised to see you at the VSTO SDR meeting last week!

I learned a lot from your blogs and I'm sure I will learn again from your new items :-)

-= Maarten =-

# March 29, 2006 3:05 AM

hansen said:

Hooray! Julie's back! Interesting and timely post too - I've been playing with the same stuff.
# March 30, 2006 4:11 AM

Anonymous said:

Hello Julie,

I am trying to figure out what are the necessary parts/relationships to create a new custom XML part.
I want to add new data to the custom xml store of an existing docx document so that it can be later bound using content controls in Word.

Basically what I do is:

- Create a new part customXML/data.xml
- Create a new properties part customXML/dataProps.xml. In this part, I set the itemID of my new data store.
- Create a new relationship of type http://schemas.openxmlformats.org/officedocument/2006/relationships/customXml from the document to the customXML/data.xml part
- Create a new relationship of type http://schemas.openxmlformats.org/officedocument/2006/relationships/customXmlProps from the customXML/data.xml part to the customXML/dataProps.xml part

I then try to access this custom part in Word using VSTO3 (Document.CustomXMLParts), without any success ...

I am wondering if I have the namespaces right. The last ECMA document that I read said that the customXml relationship was supposed to be named http://schemas.openxmlformats.org/-/customXmlData.I have tried this, too, but it doesn't work either.

Do you have any sample that shows how to do this ?

Thanks a lot for your fine blogs.

-- Cyril

# April 5, 2006 4:33 AM

Anonymous said:

Cyril,

I don't have a sample that shows how to do that, as of this moment, but I am actually working on it.

--Julie
# April 10, 2006 8:19 AM
Anonymous comments are disabled