Skip to main content
Row

CustomXmlPartCollection Interface

A collection of custom XML parts in a workbook.

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

Declaration

public interface CustomXmlPartCollection :
    OfficeCustomXmlPartCollection<ICustomXmlPart>,
    ISimpleCollection<ICustomXmlPart>,
    IEnumerable<ICustomXmlPart>,
    IEnumerable,
    ICollection

The following members return CustomXmlPartCollection objects:

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).

Workbook_CustomXmlParts

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.

View Example

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";
   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.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.

View Example

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

using (Workbook workbook = new Workbook())
{
    // Load a document.
    workbook.LoadDocument("Documents\\CustomXml.xlsx");
    // 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""?>
                            <Employees>
                                <FirstName>Stephen</FirstName>
                                <LastName>Edwards</LastName>
                            </Employees>";
    var xmlItem1 = workbook.CustomXmlParts.Add(xmlString1);

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

    // Remove the first item from the collection.
    workbook.CustomXmlParts.Remove(xmlItem1);
    // 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