|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Custom XML Parts Overview
Updated: August 2008
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
You can embed XML data in documents for some applications in the 2007 Microsoft Office system. When you embed XML data in a document, the data is named a custom XML part.
You can create and modify custom XML parts in a document by using an application-level or document-level solution in Visual Studio Tools for Office. You do not need to start the Microsoft Office application to create and modify custom XML parts.
Visual Studio Tools for Office also enables you to cache data objects in document-level customizations. This feature is different from custom XML parts, although there are some similarities. For more information, see Data Model in Document-Level Customizations.
Custom XML parts were introduced in the 2007 Microsoft Office system, along with the Open XML Formats. These formats include new XML-based file formats for Excel, PowerPoint, and Word (such as .xlsx, .pptx, and .docx). Documents in these formats consist of XML files (also named XML parts) that are organized in folders in a ZIP archive. Most of the XML parts are built-in parts that help to define the structure and the state of the document. However, documents can also contain custom XML parts, which you can use to store arbitrary XML data in the documents.
The XML file formats enable applications to work with documents in ways that are not possible with the older binary file formats (such as .xls, .ppt, and .doc). Any application that can read ZIP archives can examine and modify the contents of the documents, even if Microsoft Office is not installed.
For more information about the structure of Open XML and custom XML parts, see the following articles:
Excel 2007, Word 2007, and PowerPoint 2007 also enable you to use custom XML parts in documents that are saved in the binary file formats. However, if a document is saved in a binary format, you cannot add or modify custom XML parts without starting the Microsoft Office application.
You can create or modify custom XML parts when the document is open in the Office application, or when the document is closed—even if Microsoft Office is not installed.
Modifying XML Parts While the Office Application Is Running
You can work with custom XML parts by using a document-level customization or an application-level add-in. If you are using a document-level customization, you will typically work with custom XML parts that are in the customized document. If you are using an application-level add-in, you can create or modify custom XML parts in any document that is open in the application.
To create a custom XML part by using Visual Studio Tools for Office, add a new Microsoft.Office.Core.CustomXMLPart to the Microsoft.Office.Core.CustomXMLParts collection in the document. These classes are provided by the primary interop assemblies for the 2007 Microsoft Office system. For more information, see the following topics:
Modifying XML Parts Without Starting the Office Application
You can add or modify a custom XML part without starting Excel, PowerPoint, or Word. This is useful if you want to work with XML data in a document on a computer that does not have Microsoft Office applications installed, such as a server.
To add a custom XML part without starting Microsoft Office, there are two options:
Use classes in the Open XML Format SDK. These classes are designed to provide access to Open XML content that is specific to Office documents. For example, to add a custom XML part to an Excel workbook, you use the AddNewPart<T> method of a WorkbookPart object. For more information, see How to: Insert Custom XML to an Office Open XML Package by Using the Open XML API.
Use classes in the System.IO.Packaging namespace. The classes in this namespace provide a way to read and modify the contents of any Open XML file, including Office documents. The classes in the Open XML Format SDK are built on these classes. You can use these classes in any application that uses the .NET Framework 3.0 or later, such as a console application, a Windows Forms application, or a Windows Presentation Foundation (WPF) application. For more information, see How to: Add Custom XML Parts to Documents Without Starting Microsoft Office.
You can bind content controls in a Word 2007 solution to elements in a custom XML part. When a content control is bound to a custom XML part, the data in the custom XML part is displayed in the user interface (UI) of the content control. If a user edits text in the control, the corresponding XML element is automatically updated. Similarly, if element values in the custom XML parts are changed, the content controls that are bound to the XML elements display the new data. For more information, see Content Controls.