Thursday, 14 April 2016

XML : How to create a dataset for complex (deeper hierarchy) XML output in C#?

I never used datasets before but now I want to create a dataset which produces exactly the following XML output:

  <candidatelist>    <comment>      created 15.03.2016    </comment>    <candidates>       <candidate>        <personalinfo>          <name>Parker</name>          <firstname>Peter</firstname>          <sex>M</sex>          <birthday>19.02.1993</birthday>          <group>group1</group>          <language>E</language>          <type>H</type>        </personalinfo>        <items>          <item>item1</item>          <item>item2</item>          <item>item3</item>          <item>item4</item>          <item>item5</item>          <item>item6</item>          <item>item7</item>          <item>item8</item>        </items>      </candidate>      <candidate>        ...      </candidate>      ...    </candidates>  </candidatelist>    

My approach would be: creating a datatable "personalinfo" with the columns "name", "firstname", ... and a datatable "items" with the column "item". Then I could create a dataset named "candidate" and add both tables like this:

  DataTable table1 = new DataTable("personalinfo");  table1.Columns.Add("name");  table1.Columns.Add("firstname");  ...  table1.Rows.Add("Parker", "Peter", ...);    DataTable table2 = new DataTable("items");  table2.Columns.Add("item");  table2.Rows.Add("item1");  table2.Rows.Add("item2");  ...    DataSet set = new DataSet("candidate");  set.Tables.Add(table1);  set.Tables.Add(table2);    

But how can I add several candidates to a new set "candidates" and add that new set (together with "comment") to a set named "candidatelist"?

No comments:

Post a Comment