Skip to main content
.NET Framework 4.5.2+

CustomXmlPartCollection Interface

A collection of custom XML parts in a workbook.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v22.1.Core.dll


public interface CustomXmlPartCollection :


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.

Use one of the following properties to access a workbook’s collection of custom XML parts (ICustomXmlPart objects):

The CustomXmlPartCollection members allow you to insert, obtain, modify, or remove custom XML parts.

Add Custom XML Parts

The example below shows how to use the collection’s Add and Insert methods to add custom XML parts to a Workbook.

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

using (Workbook workbook = new Workbook())
   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";

   // Use a string to specify the content for a custom XML part.
   string xmlString = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                                 <address>1657 Wines Lane Houston, TX 77099</address>
   workbook.CustomXmlParts.Insert(1, xmlString);

   // Add a custom XML part from a file.
   XmlDocument xmlDoc = new XmlDocument();
   workbook.SaveDocument("Documents\\CustomXmlTestDoc.xlsx", DocumentFormat.Xlsx);

Access a Custom XML Part

You can use a custom XML part’s index to obtain it from the collection. Use the CustomXmlPartDocument property to access an XML document associated with the ICustomXmlPart object.

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

using (Workbook workbook = new Workbook())
    // Load a document.
    // Access a custom XML file stored in the document.
    XmlDocument xmlDoc = workbook.CustomXmlParts[0].CustomXmlPartDocument;
    // Retrieve a reference to a fish that belongs to a specific category.
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
    string xPathString = "//Fish[Category='Cod']/ScientificClassification/Reference";
    XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode(xPathString, nsmgr);
    string hLink = xmlNode.InnerText;
    // Display the obtained value in a cell as a hyperlink.
    workbook.Worksheets[0].Hyperlinks.Add(workbook.Worksheets[0].Cells["A2"], hLink, true);

Remove Custom XML Parts

Use one of the following methods to remove custom XML parts from a workbook:

using (Workbook workbook = new Workbook())
    workbook.Worksheets[0].Cells["A1"].Value = "This workbook contains custom XML parts.";

    // Add the first custom XML part.
     string xmlString1 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    var xmlItem1 = workbook.CustomXmlParts.Add(xmlString1);

    // Add the second custom XML part.
    string xmlString2 = @"<?xml version=""1.0"" encoding=""UTF-8""?>
    var xmlItem2 = workbook.CustomXmlParts.Add(xmlString2);

    // Remove the first item from the collection.
    // Use the RemoveAt method to remove an item at the specified position from the collection.
    // workbook.CustomXmlParts.RemoveAt(0);
    // Use the Clear method to remove all items from the collection.
    // workbook.CustomXmlParts.Clear();
See Also