Free Support Forum - aspose.com

Table Iteration

Hi,
I have the below xml to display in a table format. I cannot change the structure of xml as it is provided by a third party. Is there any way to show this in a table format.

<MyDetail>
	<MyDetail>
		<row>
			<entry>
				<key>C1</key>
				<value>700000.0</value>
			</entry>
			<entry>
				<key>C2</key>
				<value>aaaaa</value>
			</entry>
			<entry>
				<key>C3</key>
				<value>7.0</value>
			</entry>
		</row>
		<typeCode />
	</MyDetail>
	<MyDetail>
		<row>
			<entry>
				<key>C1</key>
				<value>800000.0</value>
			</entry>
			<entry>
				<key>C2</key>
				<value>bbbbbb</value>
			</entry>
			<entry>
				<key>C3</key>
				<value>8.0</value>
			</entry>
		</row>
		<typeCode />
	</MyDetail>
</MyDetail>

I expect the output as

C1                     C2           C3
700000.0              aaaaa        7.0
800000.0              bbbbbb       8.0

I tried normal foreach loop but it will create rows for each <entry> tag

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
Aspose - File Format APIs

Got a Complaint? File it!

Keep in touch! We’re on Twitter and Facebook

Thanks for the reply. But the issue is we are not interested to do any logic in Java.
We need to put everything in Word document itself. So I am looking for linq scripts for the same.

Can you please let me know whether this is achievable through linQ. (Without writing Java Code)

Hi there,

Thanks for your inquiry. Aspose.Words does not provide API to work with XML. You need to read the XML using Java code and write its content into table. The code shared in my previous post is C# example. Please read the following article about creating table using Aspose.Words for Java.
Creating Tables

Hi Tahir,

Is there anyway to do it in the word template itself. Can we pass a java bean instead of xml to accomplish this?

Hi there,

Thanks for your inquiry. As you shared that you cannot change the structure of XML. In this case, you need to read the XML using Java code and create the table using Aspose.Words. You may import the XML into com.aspose.words.net.System.Data.DataSet using DataSet.readXml method and use it to create the table.

Yes, you can use JavaBeans to create table. We suggest you please read the following article:

Creating Tables