CustomXmlPartCollection Class
Represents a collection of Custom XML Parts. The items are CustomXmlPart objects.
Inheritance Hierarchy
SystemObject
  Aspose.Words.MarkupCustomXmlPartCollection

Namespace: Aspose.Words.Markup
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.10
Syntax
public class CustomXmlPartCollection : IEnumerable<CustomXmlPart>, 
	IEnumerable

The CustomXmlPartCollection type exposes the following members.

Constructors
  NameDescription
Public methodCustomXmlPartCollection
Initializes a new instance of the CustomXmlPartCollection class
Properties
  NameDescription
Public propertyCode exampleCount
Gets the number of elements contained in the collection.
Public propertyCode exampleItem
Gets or sets an item at the specified index.
Methods
  NameDescription
Public methodCode exampleAdd(CustomXmlPart)
Adds an item to the collection.
Public methodCode exampleAdd(String, String)
Creates a new XML part with the specified XML and adds it to the collection.
Public methodCode exampleClear
Removes all elements from the collection.
Public methodCode exampleClone
Makes a deep copy of this collection and its items.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodCode exampleGetById
Finds and returns a custom XML part by its identifier.
Public methodCode exampleGetEnumerator
Returns an enumerator object that can be used to iterate over all items in the collection.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodCode exampleRemoveAt
Removes an item at the specified index.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Remarks

You do not normally need to create instances of this class. You can access custom XML data stored in a document via the CustomXmlParts property.

Examples
Shows how to create structured document tag with a custom XML data.
[C#]

Document doc = new Document();

// Construct an XML part that contains data and add it to the document's collection
// Once the "Developer" tab in Mircosoft Word is enabled,
// we can find elements from this collection as well as a couple defaults in the "XML Mapping Pane" 
string xmlPartId = Guid.NewGuid().ToString("B");
string xmlPartContent = "<root><text>Hello, World!</text></root>";
CustomXmlPart xmlPart = doc.CustomXmlParts.Add(xmlPartId, xmlPartContent);

// The data we entered resides in these variables
Assert.AreEqual(xmlPartContent.ToCharArray(), xmlPart.Data);
Assert.AreEqual(xmlPartId, xmlPart.Id);

// XML parts can be referenced by collection index or GUID
Assert.AreEqual(xmlPart, doc.CustomXmlParts[0]);
Assert.AreEqual(xmlPart, doc.CustomXmlParts.GetById(xmlPartId));

// Once the part is created, we can add XML schema associations like this
xmlPart.Schemas.Add("http://www.w3.org/2001/XMLSchema");

// We can also clone parts and insert them into the collection directly
CustomXmlPart xmlPartClone = xmlPart.Clone();
xmlPartClone.Id = Guid.NewGuid().ToString("B");
doc.CustomXmlParts.Add(xmlPartClone);

Assert.AreEqual(2, doc.CustomXmlParts.Count);

// Iterate through collection with an enumerator and print the contents of each part
using (IEnumerator<CustomXmlPart> enumerator = doc.CustomXmlParts.GetEnumerator())
{
    int index = 0;
    while (enumerator.MoveNext())
    {
        Console.WriteLine($"XML part index {index}, ID: {enumerator.Current.Id}");
        Console.WriteLine($"\tContent: {Encoding.UTF8.GetString(enumerator.Current.Data)}");
        index++;
    }
}

// XML parts can be removed by index
doc.CustomXmlParts.RemoveAt(1);

Assert.AreEqual(1, doc.CustomXmlParts.Count);

// The XML part collection itself can be cloned also
CustomXmlPartCollection customXmlParts = doc.CustomXmlParts.Clone();

// And all elements can be cleared like this
customXmlParts.Clear();

// Create a StructuredDocumentTag that will display the contents of our part,
// insert it into the document and save the document
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", "");

doc.FirstSection.Body.AppendChild(sdt);

doc.Save(ArtifactsDir + "SDT.CustomXml.docx");
See Also