Skip to main content
All docs
V25.1
  • DevExpress v25.1 Update — Your Feedback Matters

    Our What's New in v25.1 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

    Take the survey Not interested

    CustomXmlPartCollection Interface

    A collection of custom XML parts in a document.

    Namespace: DevExpress.XtraRichEdit.API.Native

    Assembly: DevExpress.RichEdit.v25.1.Core.dll

    NuGet Package: DevExpress.RichEdit.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 documents in DOCX and DOC format. Custom XML parts are included in the document structure but are not visible in the document.

    The image below shows the structure of a DOCX file with three custom XML parts (item1, item2, and item3).

    Document_CustomXmlParts

    Use the Document.CustomXmlParts property to access a document’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 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.Docx);
    }
    

    #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.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Xml;
    // ...
    
    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
    {
        Document document = wordProcessor.Document;
        // Load a document.
        document.LoadDocument("Documents\\CustomXmlParts.docx");
        // Access a custom XML file stored in the document.
        XmlDocument xmlDoc = document.CustomXmlParts[0].CustomXmlPartDocument;
        // Retrieve employee names from the XML file and display them in the document.
        XmlNodeList nameList = xmlDoc.GetElementsByTagName("Name");
        document.AppendText("Employee list:");
        foreach (XmlNode name in nameList)
        {
            document.AppendText("\r\n \u00B7 " + name.InnerText);
        }
    }
    

    #Remove Custom XML Parts

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

    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
    {
        Document document = wordProcessor.Document;
        document.AppendText("This document 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 = document.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 = document.CustomXmlParts.Add(xmlString2);
    
        // Remove the first item from the collection.
        document.CustomXmlParts.Remove(xmlItem1);
        // Use the RemoveAt method to remove an item at the specified position from the collection.
        // document.CustomXmlParts.RemoveAt(0);
        // Use the Clear method to remove all items from the collection.
        // document.CustomXmlParts.Clear();
    }
    
    See Also