Back to Devexpress

IWorkbook.CustomXmlParts Property

officefileapi-devexpress-dot-spreadsheet-dot-iworkbook-d297e574.md

latest4.8 KB
Original Source

IWorkbook.CustomXmlParts Property

Provides access to a workbook’s collection of custom XML parts.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
CustomXmlPartCollection CustomXmlParts { get; }
vb
ReadOnly Property CustomXmlParts As CustomXmlPartCollection

Property Value

TypeDescription
CustomXmlPartCollection

A collection of custom XML parts.

|

Remarks

You can embed arbitrary XML data (called custom XML parts) in a workbook. Custom XML parts are included in the document structure but are not visible in the document.

The image below shows the structure of an XLSX file with three custom XML parts (item1, item2, and item3).

The following spreadsheet file formats support custom XML parts:

  • XLSX;
  • XLSM;
  • XLTX;
  • XLTM;
  • XLSB;
  • XLS;
  • XLT.

The IWorkbook.CustomXmlParts property provides access to the CustomXmlPartCollection collection that stores custom XML parts. Use the collection’s members to insert, obtain, modify, or remove custom XML parts.

The example below shows how to add custom XML parts to a workbook.

View Example

csharp
using DevExpress.Spreadsheet;
using System.Xml;
// ...

IWorkbook workbook = spreadsheetControl.Document;
workbook.BeginUpdate();
workbook.Worksheets[0].Cells["A1"].Value = "This workbook contains custom XML parts.";

// Add an empty custom XML part.
ICustomXmlPart xmlItem = workbook.CustomXmlParts.Add();
// Populate the XML part with content.
XmlElement elem = xmlItem.CustomXmlPartDocument.CreateElement("Person");
elem.InnerText = "Stephen Edwards";
xmlItem.CustomXmlPartDocument.AppendChild(elem);

// Use a string to specify the content for a custom XML part.
string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                <whitepaper>
                    <contact>
                        <firstname>Roger</firstname>
                        <lastname>Edwards</lastname>
                        <phone>832-433-0025</phone>
                        <address>1657 Wines Lane Houston, TX 77099</address>
                    </contact>
                    <date>2016-05-18</date>
                </whitepaper>";
workbook.CustomXmlParts.Insert(1, xmlString);

// Add a custom XML part from a file.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Documents\\fishes.xml");
workbook.CustomXmlParts.Add(xmlDoc);
workbook.EndUpdate();

workbook.SaveDocument("Documents\\CustomXmlTestDoc.xlsx", DocumentFormat.Xlsx);
vb
Imports DevExpress.Spreadsheet
Imports System.Xml
' ...

Dim workbook As IWorkbook = spreadsheetControl.Document
workbook.BeginUpdate()
workbook.Worksheets(0).Cells("A1").Value = "This workbook contains custom XML parts."

' Add an empty custom XML part.
Dim xmlItem As ICustomXmlPart = workbook.CustomXmlParts.Add()
' Populate the XML part with content.
Dim elem As XmlElement = xmlItem.CustomXmlPartDocument.CreateElement("Person")
elem.InnerText = "Stephen Edwards"
xmlItem.CustomXmlPartDocument.AppendChild(elem)

' Use a string to specify the content for a custom XML part.
Dim xmlString As String = "<?xml version=""1.0"" encoding=""UTF-8""?>
                <whitepaper>
                    <contact>
                        <firstname>Roger</firstname>
                        <lastname>Edwards</lastname>
                        <phone>832-433-0025</phone>
                        <address>1657 Wines Lane Houston, TX 77099</address>
                    </contact>
                    <date>2016-05-18</date>
                </whitepaper>"
workbook.CustomXmlParts.Insert(1, xmlString)

' Add a custom XML part from a file.
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("Documents\fishes.xml")
workbook.CustomXmlParts.Add(xmlDoc)
workbook.EndUpdate()

workbook.SaveDocument("Documents\CustomXmlTestDoc.xlsx", DocumentFormat.Xlsx)

See Also

IWorkbook Interface

IWorkbook Members

DevExpress.Spreadsheet Namespace