Tuesday, September 29, 2009

Using VSTO to access the CustomXML of the word document file

Here is the code for accessing the CustomXML file of the word document file


foreach (System.IO.Packaging.PackageRelationship relationship in myPackage.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"))
            {
                Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);
                documentPart = myPackage.GetPart(documentUri);
              
                foreach (PackageRelationship objPckRel in documentPart.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml"))
                {
                    partUri = PackUriHelper.ResolvePartUri(objPckRel.SourceUri ,objPckRel.TargetUri);
                    objPackPart = myPackage.GetPart(partUri);
                    doc = new XmlDocument();
                    doc.Load(objPackPart.GetStream());
                    try
                    {
                       objNode = doc.SelectSingleNode("/root[1]/ProjectData[1]");
                      string strXML = objNode.OuterXml;
                      break;

                    }
                    catch (Exception ex)
                    {
                        //do nothing
                    }
                }
            }   

No comments:

Post a Comment