Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Add Custom XML Parts to Documents Without Starting Microsoft Office

Applies to

The information in this topic applies only to the specified versions of Microsoft Office.

Microsoft Office version

  • Excel 2007

  • PowerPoint 2007

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can add a custom XML part to a document without starting Microsoft Office Excel, Microsoft Office PowerPoint, or Microsoft Office Word. This is useful if you want to store XML data in a document on a computer that does not have Microsoft Office installed, such as a server. For more information, see Custom XML Parts Overview.

The document must be in one of the Open XML Formats, such as .docx, .xlsx, or .pptx. You cannot access custom XML parts in the binary file formats without starting the Microsoft Office application.

To add a custom XML part without starting Microsoft Office, use the Package and PackagePart classes. For more information about these classes, see Documents in Windows Presentation Foundation.

To add a custom XML part to a document without starting Microsoft Office

  1. Create an XmlDocument object that represents the XML that you want to add to a custom XML part.

    string xmlString =
        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
        "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
            "<employee>" +
                "<name>Karina Leal</name>" +
                "<hireDate>1999-04-01</hireDate>" +
                "<title>Manager</title>" +
            "</employee>" +
        "</employees>";
    
    StringReader reader = new StringReader(xmlString);
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(reader);
    
  2. Create a new PackagePart that represents the custom XML part that you are adding to the document. Then, save the XML from the XmlDocument object you created earlier into the new custom XML part.

    using (Package package = Package.Open(fullDocumentPath, FileMode.Open,
        FileAccess.ReadWrite))
    {
        Uri uriPartTarget = new Uri("/customXml/employee1.xml", UriKind.Relative);
    
        if (!package.PartExists(uriPartTarget))
        {
            PackagePart customXml = package.CreatePart(uriPartTarget,
                "application/vnd.openxmlformats-officedocument.customXmlProperties+xml");
    
            using (Stream partStream = customXml.GetStream(FileMode.Create,
                FileAccess.ReadWrite))
            {
                xmlDoc.Save(partStream);
            }
        }
    }
    

The following code example adds a custom XML part to a Word document named Employees.docx that is located in the %UserProfile%\My Documents folder (for Windows XP and earlier) or the %UserProfile%\Documents folder (for Windows Vista).

This example is intended to be used in a console application, but you can modify it to work in any application that works with the .NET Framework 3.0 or later, such as a Windows Presentation Foundation (WPF) application.

To modify this example to work with an Excel or PowerPoint document, modify the Main method to pass in the full path of the document to the AddCustomXmlPart method.

static void Main(string[] args)
{
    AddCustomXmlPart(Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + @"\Employees.docx");
}

private static void AddCustomXmlPart(string fullDocumentPath)
{
    XmlDocument xmlDoc = GetXmlDocumentFromString();

    if (xmlDoc != null)
    {
        using (Package package = Package.Open(fullDocumentPath, FileMode.Open,
            FileAccess.ReadWrite))
        {
            Uri uriPartTarget = new Uri("/customXml/employee1.xml", UriKind.Relative);

            if (!package.PartExists(uriPartTarget))
            {
                PackagePart customXml = package.CreatePart(uriPartTarget,
                    "application/vnd.openxmlformats-officedocument.customXmlProperties+xml");

                using (Stream partStream = customXml.GetStream(FileMode.Create,
                    FileAccess.ReadWrite))
                {
                    xmlDoc.Save(partStream);
                }
            }
        }
    }
}

private static XmlDocument GetXmlDocumentFromString()
{
    string xmlString =
        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
        "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
            "<employee>" +
                "<name>Karina Leal</name>" +
                "<hireDate>1999-04-01</hireDate>" +
                "<title>Manager</title>" +
            "</employee>" +
        "</employees>";

    StringReader reader = new StringReader(xmlString);
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(reader);
    return xmlDoc;
}

  • This example requires a reference to the WindowsBase.dll assembly. This assembly is included in the .NET Framework, beginning with version 3.0.

  • This example assumes that you have using (for C#) or Imports (for Visual Basic) statements for the following namespaces:

    • System.IO

    • System.IO.Packaging

    • System.Xml

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft