Hi there,
Thanks for your inquiry. In your case, we suggest you please import the XML into DataSet using DataSet.ReadXml method and create the table using following code example. Hope this helps you.
Moreover, we suggest you please read following article:
How to Build a Table from a DataTable
// Create a new document.
Document doc = new Document();
// We want to rotate the page landscape as we expect a wide table.
doc.FirstSection.PageSetup.Orientation = Orientation.Landscape;
DataSet ds = new DataSet();
ds.ReadXml(MyDir + "input.xml");
// Retrieve the data from our data source which is stored as a DataTable.
DataTable dataTable = ds.Tables["entry"];
DataView view = new DataView(dataTable);
DataTable distinctValues = view.ToTable(true, "key");
Table table = new Table(doc);
Row row = new Row(doc);
// Create heading of table
foreach (DataRow dataRow in distinctValues.Rows)
{
foreach (object item in dataRow.ItemArray)
{
Cell cell = new Cell(doc);
row.Cells.Add(cell);
cell.EnsureMinimum();
cell.FirstParagraph.AppendChild(new Run(doc, item.ToString()));
}
}
table.Rows.Add(row);
//Create table's row for key value data
for (int j = 0; j < dataTable.Rows.Count / distinctValues.Rows.Count; j++)
{
Row r = new Row(doc);
for (int i = 0; i < distinctValues.Rows.Count; i++)
{
Cell cell = new Cell(doc);
r.Cells.Add(cell);
cell.EnsureMinimum();
}
table.Rows.Add(r);
}
//Fill table's cell with data
for (int column = 0; column < table.FirstRow.Cells.Count; column++)
{
string expression = "key = '" + table.FirstRow.Cells[column].FirstParagraph.ToString(SaveFormat.Text).Trim() + "'";
DataTable tableRows = view.ToTable(true, "key", "value");
DataRow[] datarows = tableRows.Select(expression);
for (int tablerow = 0; tablerow < datarows.Length; tablerow++)
{
table.Rows[tablerow + 1].Cells[column].FirstParagraph.AppendChild(new Run(doc, datarows[tablerow].ItemArray[1].ToString()));
}
}
doc.FirstSection.Body.AppendChild(table);
// Save the output document.
doc.Save(MyDir + "Table.FromDataTable Out.docx");
Best Regards,
Tahir Manzoor