officefileapi-devexpress-dot-spreadsheet-bdac7ef3.md
A collection of custom XML parts in a workbook.
Namespace : DevExpress.Spreadsheet
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public interface CustomXmlPartCollection :
OfficeCustomXmlPartCollection<ICustomXmlPart>,
ISimpleCollection<ICustomXmlPart>,
IEnumerable<ICustomXmlPart>,
IEnumerable,
ICollection
Public Interface CustomXmlPartCollection
Inherits OfficeCustomXmlPartCollection(Of ICustomXmlPart),
ISimpleCollection(Of ICustomXmlPart),
IEnumerable(Of ICustomXmlPart),
IEnumerable,
ICollection
The following members return CustomXmlPartCollection objects:
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:
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.
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";
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);
}
Imports DevExpress.Spreadsheet
Imports System.Xml
' ...
Using workbook As New Workbook()
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.SaveDocument("Documents\CustomXmlTestDoc.xlsx", DocumentFormat.Xlsx)
End Using
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.
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);
}
Imports DevExpress.Spreadsheet
Imports System.Xml
' ...
Using workbook As New Workbook()
' Load a document.
workbook.LoadDocument("Documents\CustomXml.xlsx")
' Access a custom XML file stored in the document.
Dim xmlDoc As XmlDocument = workbook.CustomXmlParts(0).CustomXmlPartDocument
' Retrieve a reference to a fish that belongs to a specific category.
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
Dim xPathString As String = "//Fish[Category='Cod']/ScientificClassification/Reference"
Dim xmlNode As XmlNode = xmlDoc.DocumentElement.SelectSingleNode(xPathString, nsmgr)
Dim hLink As String = xmlNode.InnerText
' Display the obtained value in a cell as a hyperlink.
workbook.Worksheets(0).Hyperlinks.Add(workbook.Worksheets(0).Cells("A2"), hLink, True)
End Using
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();
}
Using workbook As New Workbook()
workbook.Worksheets(0).Cells("A1").Value = "This workbook contains custom XML parts."
' Add the first custom XML part.
Dim xmlString1 As String = "<?xml version=""1.0"" encoding=""UTF-8""?>
<Employees>
<FirstName>Stephen</FirstName>
<LastName>Edwards</LastName>
</Employees>"
Dim xmlItem1 = workbook.CustomXmlParts.Add(xmlString1)
' Add the second custom XML part.
Dim xmlString2 As String = "<?xml version=""1.0"" encoding=""UTF-8""?>
<Employees>
<FirstName>Andrew</FirstName>
<LastName>Fuller</LastName>
</Employees>"
Dim 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();
End Using
See Also