Aspose::Words::Markup::CustomXmlPartCollection class
Contents
[
Hide
]CustomXmlPartCollection class
Represents a collection of Custom XML Parts. The items are CustomXmlPart objects. To learn more, visit the Structured Document Tags or Content Control documentation article.
class CustomXmlPartCollection : public System::Collections::Generic::IEnumerable<System::SharedPtr<Aspose::Words::Markup::CustomXmlPart>>
Methods
Method | Description |
---|---|
Add(const System::SharedPtr<Aspose::Words::Markup::CustomXmlPart>&) | Adds an item to the collection. |
Add(const System::String&, const System::String&) | Creates a new XML part with the specified XML and adds it to the collection. |
begin() | |
begin() const | |
cbegin() const | |
cend() const | |
Clear() | Removes all elements from the collection. |
Clone() | Makes a deep copy of this collection and its items. |
CustomXmlPartCollection() | |
end() | |
end() const | |
get_Count() | Gets the number of elements contained in the collection. |
GetById(const System::String&) | Finds and returns a custom XML part by its identifier. |
GetEnumerator() override | Returns an enumerator object that can be used to iterate over all items in the collection. |
GetType() const override | |
idx_get(int32_t) | Gets or sets an item at the specified index. |
idx_set(int32_t, const System::SharedPtr<Aspose::Words::Markup::CustomXmlPart>&) | Gets or sets an item at the specified index. |
Is(const System::TypeInfo&) const override | |
RemoveAt(int32_t) | Removes an item at the specified index. |
static Type() | |
virtualizeBeginConstIterator() const override | |
virtualizeBeginIterator() override | |
virtualizeEndConstIterator() const override | |
virtualizeEndIterator() override |
Typedefs
Typedef | Description |
---|---|
const_iterator | |
iterator | |
iterator_holder_type | |
virtualized_iterator | |
virtualized_iterator_element |
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 a structured document tag with custom XML data.
auto doc = System::MakeObject<Aspose::Words::Document>();
// Construct an XML part that contains data and add it to the document's collection.
// If we enable the "Developer" tab in Microsoft Word,
// we can find elements from this collection in the "XML Mapping Pane", along with a few default elements.
System::String xmlPartId = System::Guid::NewGuid().ToString(u"B");
System::String xmlPartContent = u"<root><text>Hello world!</text></root>";
System::SharedPtr<Aspose::Words::Markup::CustomXmlPart> xmlPart = doc->get_CustomXmlParts()->Add(xmlPartId, xmlPartContent);
ASPOSE_ASSERT_EQ(System::Text::Encoding::get_ASCII()->GetBytes(xmlPartContent), xmlPart->get_Data());
ASSERT_EQ(xmlPartId, xmlPart->get_Id());
// Below are two ways to refer to XML parts.
// 1 - By an index in the custom XML part collection:
ASPOSE_ASSERT_EQ(xmlPart, doc->get_CustomXmlParts()->idx_get(0));
// 2 - By GUID:
ASPOSE_ASSERT_EQ(xmlPart, doc->get_CustomXmlParts()->GetById(xmlPartId));
// Add an XML schema association.
xmlPart->get_Schemas()->Add(u"http://www.w3.org/2001/XMLSchema");
// Clone a part, and then insert it into the collection.
System::SharedPtr<Aspose::Words::Markup::CustomXmlPart> xmlPartClone = xmlPart->Clone();
xmlPartClone->set_Id(System::Guid::NewGuid().ToString(u"B"));
doc->get_CustomXmlParts()->Add(xmlPartClone);
ASSERT_EQ(2, doc->get_CustomXmlParts()->get_Count());
// Iterate through the collection and print the contents of each part.
{
System::SharedPtr<System::Collections::Generic::IEnumerator<System::SharedPtr<Aspose::Words::Markup::CustomXmlPart>>> enumerator = doc->get_CustomXmlParts()->GetEnumerator();
int32_t index = 0;
while (enumerator->MoveNext())
{
std::cout << System::String::Format(u"XML part index {0}, ID: {1}", index, enumerator->get_Current()->get_Id()) << std::endl;
std::cout << System::String::Format(u"\tContent: {0}", System::Text::Encoding::get_UTF8()->GetString(enumerator->get_Current()->get_Data())) << std::endl;
index++;
}
}
// Use the "RemoveAt" method to remove the cloned part by index.
doc->get_CustomXmlParts()->RemoveAt(1);
ASSERT_EQ(1, doc->get_CustomXmlParts()->get_Count());
// Clone the XML parts collection, and then use the "Clear" method to remove all its elements at once.
System::SharedPtr<Aspose::Words::Markup::CustomXmlPartCollection> customXmlParts = doc->get_CustomXmlParts()->Clone();
customXmlParts->Clear();
// Create a structured document tag that will display our part's contents and insert it into the document body.
auto tag = System::MakeObject<Aspose::Words::Markup::StructuredDocumentTag>(doc, Aspose::Words::Markup::SdtType::PlainText, Aspose::Words::Markup::MarkupLevel::Block);
tag->get_XmlMapping()->SetMapping(xmlPart, u"/root[1]/text[1]", System::String::Empty);
doc->get_FirstSection()->get_Body()->AppendChild<System::SharedPtr<Aspose::Words::Markup::StructuredDocumentTag>>(tag);
doc->Save(get_ArtifactsDir() + u"StructuredDocumentTag.CustomXml.docx");
See Also
- Namespace Aspose::Words::Markup
- Library Aspose.Words for C++