Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all!

I am trying to generate an XML file using the values from the word document table. The document has multiple tables having values to generate an xml file against some attributes. Now hierarchy should be maintained. For example if my node is "cw" and child node is "page", I have to generate n no of pages with attributes read from the doc file. So guys I need help as this is very urgent. I am providing the work I did and a sample file.

XmlTextWriter writer = new XmlTextWriter("E:\\test.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
XmlDocument doc1 = new XmlDocument();

C#
foreach (Table tb in doc.Tables)
{
    if (tb.Columns.Count == 1)
    {
        continue;
    }

    //loop to retrieve labels and attribute of childnode

    for (int row = 1; row <= tb.Rows.Count; row++)
    {
        var cell = tb.Cell(row, 1);
        var text = cell.Range.Text;
        var cellval = tb.Cell(row, 2);
        var textval = cellval.Range.Text;

        LabelString = LabelString + '\t' + text.ToString();
        AttribString = AttribString + '\t' + textval.ToString();
    }


}

node data 1
node name Name1
Page Name Lab Test1
Page No 1
Page Type Labs


node data 1 - 1
node name Name2
Page Name Lab Test2
Page No 1
Page Type experiment1

node data 1 - 1
node name Name2
Page Name Lab Test3
Page No 2
Page Type experiment1

node data 1 - 2
node name Name4
Page Name Lab Test4
Page No 1
Page Type experiment2

XML
<node data="nd01" name="Name1" level="1">
  <page name="Lab Test1" no="pg01" type="experiment1" />
  <node data="nd01" name="Name2" level="2">
    <page name="Lab Test2" no="pg01" type="experiment1" />
    <page name="Lab Test3" no="pg02" type="experiment1" />
  </node>
  <node data="nd02" name="Name4" level="2"> 
    <page name="Lab Test4" no="pg01" type="experiment2" />
  </node>
</node>
Posted
Updated 20-Jan-15 23:13pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900