创建映射到自定义 Xml 部件的表重复部分
本教程演示如何使用 Aspose.Words for .NET 创建一个表,其中的重复部分映射到 Word 文档中的自定义 Xml 部件。重复部分允许您根据自定义 Xml 部件中存储的 XML 数据动态添加行。
先决条件
要学习本教程,您需要具备以下条件:
- 已安装 Aspose.Words for .NET 库。
- C# 和 Word 文档文字处理的基础知识。
第 1 步:设置文档目录
首先设置文档目录的路径。代替"YOUR DOCUMENT DIRECTORY"
与要保存文档的目录的实际路径。
string dataDir = "YOUR DOCUMENT DIRECTORY";
第 2 步:创建文档和 DocumentBuilder
创建一个新实例Document
类和一个DocumentBuilder
构建文档的内容。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
步骤 3:将自定义 XML 数据添加到 CustomXmlPart
创建一个CustomXmlPart
并向其中添加自定义 XML 数据。在此示例中,我们创建一个 XML 字符串,表示包含标题和作者的书籍集合。
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>");
第四步:创建表和表结构
开始使用创建表StartTable
的方法DocumentBuilder
。使用以下命令添加表格单元格和内容InsertCell
和Write
方法。
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Title");
builder.InsertCell();
builder.Write("Author");
builder.EndRow();
builder.EndTable();
步骤 5:创建映射到自定义 XML 的重复部分
创建一个StructuredDocumentTag
和SdtType.RepeatingSection
来表示重复部分。使用以下命令设置重复部分的 XML 映射SetMapping
的方法XmlMapping
财产。在此示例中,我们将重复部分映射到/books[1]/book
.
StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
第 6 步:创建重复部分项并添加单元格
创建一个StructuredDocumentTag
和SdtType.RepeatingSectionItem
代表重复节项。将其作为子项附加到重复部分。
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
创建一个Row
表示重复部分中的每个项目并将其附加到重复部分项目。
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
步骤 7:在重复部分添加内容控件
创造StructuredDocumentTag
对象与SdtType.PlainText
代表标题和作者内容控件。使用以下命令为每个内容控件设置 XML 映射SetMapping
的方法XmlMapping
财产。在此示例中,我们将标题控件映射到/books[1]/book[1]/title[1]
和作者控制/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);
第 8 步:保存文档
使用命令将修改后的文档保存到指定目录Save
方法。提供所需的文件名和适当的文件扩展名。在此示例中,我们将文档另存为“WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx”。
doc.Save(dataDir + "WorkingWithSdt.CreatingTableRepeatingSectionMappedToCustomXmlPart.docx");
使用 Aspose.Words for .NET 创建映射到自定义 Xml 部件的表重复部分的示例源代码
//文档目录的路径
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");
就是这样!您已使用 Aspose.Words for .NET 成功创建了一个表,其中的重复部分映射到 Word 文档中的 CustomXmlPart。