Skip to main content
.NET Framework 4.5.2+

OfficeCustomXmlPartCollection<T>.Insert(Int32, XmlDocument) Method

Inserts the specified XML document at the specified position in the collection.

Namespace: DevExpress.Office

Assembly: DevExpress.Office.v22.2.Core.dll

NuGet Package: DevExpress.Office.Core

Declaration

T Insert(
    int index,
    XmlDocument document
)

Parameters

Name Type Description
index Int32

A zero-based index that specifies the position where to insert a custom XML part.

document XmlDocument

An XML document to add to the collection.

Returns

Type Description
T

An object that exposes the ICustomXmlPart interface and specifies a newly created custom XML part.

Remarks

The examples below show how to add custom XML parts to a workbook and a text document.

Add Custom XML Parts to a DOCX Document

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Xml;
// ...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    Document document = wordProcessor.Document;
    document.AppendText("This document contains custom XML parts.");
    // Add an empty custom XML part.
    ICustomXmlPart xmlItem = document.CustomXmlParts.Add();
    // Populate the XML part with content.
    XmlElement elem = xmlItem.CustomXmlPartDocument.CreateElement("Employees");
    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""?>
                            <Employees>
                                <FirstName>Stephen</FirstName>
                                <LastName>Edwards</LastName>
                                <Address>4726 - 11th Ave. N.E.</Address>
                                <City>Seattle</City>
                                <Region>WA</Region>
                                <PostalCode>98122</PostalCode>
                                <Country>USA</Country>
                            </Employees>";
    document.CustomXmlParts.Insert(1, xmlString);

    // Add a custom XML part from a file.
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("Documents\\Employees.xml");
    document.CustomXmlParts.Add(xmlDoc);
    document.SaveDocument("Documents\\CustomXmlTestDoc.docx", DocumentFormat.OpenXml);
}

Add Custom XML Parts to an XLSX Document

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);
}

Use the Remove, RemoveAt and Clear methods to remove custom XML parts from a document.

See Also