How to: Add Custom XML Parts to Documents Without Starting Microsoft Office
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.
Applies to: The information in this topic applies to the following applications: Excel 2007 and Excel 2010; PowerPoint 2007 and PowerPoint 2010; Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.
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 WPF.
To add a custom XML part to a document without starting Microsoft Office
Create an XmlDocument object that represents the XML that you want to add to a custom XML part.
Dim xmlString As String = _ "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _ "<employees https://schemas.microsoft.com/vsto/samples"">" & _ "<employee>" & _ "<name>Karina Leal</name>" & _ "<hireDate>1999-04-01</hireDate>" & _ "<title>Manager</title>" & _ "</employee>" & _ "</employees>" Dim reader As StringReader = New StringReader(xmlString) Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(reader)
string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<employees xmlns=\"https://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);
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 As Package = package.Open(fullDocumentPath, FileMode.Open, _ FileAccess.ReadWrite) Dim uriPartTarget As Uri = New Uri("/customXml/employee1.xml", UriKind.Relative) If Not package.PartExists(uriPartTarget) Then Dim customXml As PackagePart = package.CreatePart(uriPartTarget, _ "application/vnd.openxmlformats-officedocument.customXmlProperties+xml") Using partStream As Stream = customXml.GetStream(FileMode.Create, _ FileAccess.ReadWrite) xmlDoc.Save(partStream) End Using End If End Using
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); } } }
Example
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.
Sub Main()
AddCustomXmlPart(Environment.GetFolderPath( _
Environment.SpecialFolder.MyDocuments) & "\Employees.docx")
End Sub
Private Sub AddCustomXmlPart(ByVal fullDocumentPath As String)
Dim xmlDoc As XmlDocument = GetXmlDocumentFromString()
If xmlDoc IsNot Nothing Then
Using package As Package = package.Open(fullDocumentPath, FileMode.Open, _
FileAccess.ReadWrite)
Dim uriPartTarget As Uri = New Uri("/customXml/employee1.xml", UriKind.Relative)
If Not package.PartExists(uriPartTarget) Then
Dim customXml As PackagePart = package.CreatePart(uriPartTarget, _
"application/vnd.openxmlformats-officedocument.customXmlProperties+xml")
Using partStream As Stream = customXml.GetStream(FileMode.Create, _
FileAccess.ReadWrite)
xmlDoc.Save(partStream)
End Using
End If
End Using
End If
End Sub
Private Function GetXmlDocumentFromString() As XmlDocument
Dim xmlString As String = _
"<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
"<employees https://schemas.microsoft.com/vsto/samples"">" & _
"<employee>" & _
"<name>Karina Leal</name>" & _
"<hireDate>1999-04-01</hireDate>" & _
"<title>Manager</title>" & _
"</employee>" & _
"</employees>"
Dim reader As StringReader = New StringReader(xmlString)
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(reader)
Return xmlDoc
End Function
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=\"https://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;
}
Compiling the Code
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
See Also
Tasks
How to: Add Custom XML Parts to Document-Level Customizations
How to: Add Custom XML Parts to Documents by Using Application-Level Add-Ins