CustomXmlPart Class |
Namespace: Aspose.Words.Markup
The CustomXmlPart type exposes the following members.
Name | Description | |
---|---|---|
![]() | CustomXmlPart | Initializes a new instance of the CustomXmlPart class |
Name | Description | |
---|---|---|
![]() ![]() | Data |
Gets or sets the XML content of this Custom XML Data Storage Part.
|
![]() ![]() | Id |
Gets or sets the string that identifies this custom XML part within an OOXML document.
|
![]() ![]() | Schemas |
Specifies the set of XML schemas that are associated with this custom XML part.
|
Name | Description | |
---|---|---|
![]() ![]() | Clone |
Makes a "deep enough" copy of the object.
Does not duplicate the bytes of the Data value.
|
![]() | Equals | (Inherited from Object.) |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() | ToString | (Inherited from Object.) |
A DOCX or DOC document can contain one or more Custom XML Data Storage parts. Aspose.Words preserves and allows to create and extract Custom XML Data via the CustomXmlParts collection.
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(Encoding.ASCII.GetBytes(xmlPartContent), 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 + "StructuredDocumentTag.CustomXml.docx");