Creating Table Repeating Section Mapped To Custom Xml Part

This tutorial demonstrates how to create a table with a repeating section mapped to a Custom Xml Part in a Word document using Aspose.Words for .NET. The repeating section allows you to dynamically add rows based on the XML data stored in the Custom Xml Part.

Prerequisites

To follow this tutorial, you need to have the following:

  • Aspose.Words for .NET library installed.
  • Basic knowledge of C# and Words Processing with Word documents.

Step 1: Set up the Document Directory

Begin by setting up the path to your document directory. Replace "YOUR DOCUMENT DIRECTORY" with the actual path to the directory where you want to save the document.

string dataDir = "YOUR DOCUMENT DIRECTORY";

Step 2: Create a Document and DocumentBuilder

Create a new instance of the Document class and a DocumentBuilder to build the document’s content.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Step 3: Add Custom XML Data to a CustomXmlPart

Create a CustomXmlPart and add custom XML data to it. In this example, we create an XML string representing a collection of books with their titles and authors.

CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
	"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
	"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
	"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");

Step 4: Create a Table and Table Structure

Start creating a table using the StartTable method of the DocumentBuilder. Add table cells and content using the InsertCell and Write methods.

Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();

Step 5: Create the Repeating Section Mapped to Custom XML

Create a StructuredDocumentTag with SdtType.RepeatingSection to represent the repeating section. Set the XML mapping for the repeating section using the SetMapping method of the XmlMapping property. In this example, we map the repeating section to /books[1]/book.

StructuredDocumentTag repeatingSectionSdt =
	new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);

Step 6: Create the Repeating Section Item and Add Cells

Create a StructuredDocumentTag with SdtType.RepeatingSectionItem to represent the repeating section item. Append it as a child to the repeating section.

StructuredDocumentTag repeatingSectionItemSdt = 
	new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);

Create a Row to represent each item in the repeating section and append it to the repeating section item.

Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);

Step 7: Add Content Controls within the Repeating Section

Create StructuredDocumentTag objects with SdtType.PlainText

to represent the title and author content controls. Set the XML mapping for each content control using the SetMapping method of the XmlMapping property. In this example, we map the title control to /books[1]/book[1]/title[1] and the author control to /books[1]/book[1]/author[1].

StructuredDocumentTag titleSdt =
	new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);

StructuredDocumentTag authorSdt =
	new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);

Step 8: Save the Document

Save the modified document to the specified directory using the Save method. Provide the desired filename with the appropriate file extension. In this example, we save the document as “WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx”.

doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");

Example source code for Creating Table Repeating Section Mapped To Custom Xml Part using Aspose.Words for .NET

	// Path to your document directory 
	string dataDir = "YOUR DOCUMENT DIRECTORY";

	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);
	CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
		"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
		"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
		"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");
	Table table = builder.StartTable();
	builder.InsertCell();
	builder.Write("Title");
	builder.InsertCell();
	builder.Write("Author");
	builder.EndRow();
	builder.EndTable();
	StructuredDocumentTag repeatingSectionSdt =
		new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
	repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
	table.AppendChild(repeatingSectionSdt);
	StructuredDocumentTag repeatingSectionItemSdt = 
		new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
	repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
	Row row = new Row(doc);
	repeatingSectionItemSdt.AppendChild(row);
	StructuredDocumentTag titleSdt =
		new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
	titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
	row.AppendChild(titleSdt);
	StructuredDocumentTag authorSdt =
		new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
	authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
	row.AppendChild(authorSdt);
	doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");

That’s it! You have successfully created a table with a repeating section mapped to a CustomXmlPart in your Word document using Aspose.Words for .NET.