All docs
V21.1
21.2 (EAP/Beta)
21.1
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.
.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

OfficeCustomXmlPartCollection<T>.Add(String) Method

Adds an item with the specified XML content to the collection.

Namespace: DevExpress.Office

Assembly: DevExpress.Office.v21.1.Core.dll

Declaration

T Add(
    string content
)

Parameters

Name Type Description
content String

An XML string to store in the custom XML part.

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

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