How can I create a nested XML file from a dataset or dataset without looping?

I want to create nested XML file from DataTable without loop.

DataTable: Employee enter image description here

I have tried the following piece of code

DataSet ds=new DataSet ("EmployeeDetails");
DataTable dtConvert = datatable to be converted
ds.Tables.Add(dtConvert);
ds.WriteXml("sample.txt");

      

and got XML that looks like,

<EmployeeDetails>
  <Employee>
    <name>John</name>
    <city>chennai</city>
    <state>Tamilnadu</state>
    <country>India</country>
  </Employee>
  <Employee>
    <name>David</name>
    <city>Bangalore</city>
    <state>Karnataka</state>
    <country>India</country>
  </Employee>
</EmployeeDetails>

      

But the XML format I want is -

<EmployeeDetails>
  <Employee>
    <name>John</name>
    <address>
        <city>chennai</city>
        <state>Tamilnadu</state>
        <country>India</country>
    </address>
  </Employee>
  <Employee>
    <name>David</name>
    <address>
        <city>Bangalore</city>
        <state>Karnataka</state>
        <country>India</country>
    </address>
  </Employee>
</EmployeeDetails>

      

Can anyone help me to do this in the best way?

+1


source to share


2 answers


        DataSet ds = new DataSet("EmployeeList");

        //create table address
        DataTable address = new DataTable("Address");
        DataColumn column;

        //add column id
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName  = "ID";
        address.Columns.Add(column);
        //address.PrimaryKey = new DataColumn[1] { column };

        //add column City
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "City";
        address.Columns.Add(column);

        //add column State
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "State";
        address.Columns.Add(column);

        //add column Country
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "Country";
        address.Columns.Add(column);

        ds.Tables.Add(address); //add table address to dataset

        //create table employee
        DataTable employee = new DataTable("Employee");

        //add column ID
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.Int32");
        column.ColumnName = "ID";
        employee.Columns.Add(column);
        employee.PrimaryKey = new DataColumn[1] { column };

        //add column Name
        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "Name";
        employee.Columns.Add(column);

        //add column Address
        //column = new DataColumn();
        //column.DataType = System.Type.GetType("System.Int32");
        //column.ColumnName = "Address";
        //employee.Columns.Add(column);

        ds.Tables.Add(employee); //add table employee to dataset

        //set foreign key constraint
        //ForeignKeyConstraint fk = new ForeignKeyConstraint("AddressFK", 
        //ds.Tables["Address"].Columns["ID"], ds.Tables["Employee"].Columns["Address"]);

        //fk.DeleteRule = Rule.None;
        //ds.Tables["Employee"].Constraints.Add(fk);

        //
        // fill data to data set
        //
        DataRow row;
        row = address.NewRow();
        row["ID"] = 1;
        row["City"] = "test";
        row["State"] = "test";
        row["Country"] = "test";

        address.Rows.Add(row);

        row = employee.NewRow();
        row["Name"] = "abc";
        row["ID"] = 1;
        //row["Address"] = 1;

        employee.Rows.Add(row);

        DataRelation relation = ds.Relations.Add("relation", ds.Tables["Employee"].Columns["ID"], ds.Tables["Address"].Columns["ID"]);
        relation.Nested = true;

        ds.WriteXml("test.txt"); //create xml from dataset

      

I am trying to create 2 tables here Employee

and Address

. Address

It has 4 columns ID

- foreign key that refers to a primary key Employee

, State

, City

, Country

. Employee

has 2 columns Name

, ID

is the primary key. Then add these tables to the dataset ds

. Finally, create xml from ds

.

P / S: I am writing this in plain text (no IDE, because this machine has no IDE :(, so if any typo or syntax error please let me know)

UPDATE I updated the code, now the XML output looks like this:

<?xml version="1.0" standalone="yes"?>
<EmployeeList>
  <Employee>
    <ID>1</ID>
    <Name>abc</Name>
    <Address>
      <ID>1</ID>
      <City>test</City>
      <State>test</State>
      <Country>test</Country>
    </Address>
  </Employee>
</EmployeeList>

      



I got it wrong with the relationship since lately, it must be a foreign key in Address

that referencing the primary key Employee

(in this case I am a custom column ID

)

UPDATE2 to exclude id from Address

add this line right before the callds.WriteXML("test.xml")

ds.Tables["Address"].Columns["ID"].ColumnMapping = MappingType.Hidden;

      

You can also add the following line to exclude the ID from the employee:

ds.Tables["Employee"].Columns["ID"].ColumnMapping = MappingType.Hidden;

      

+3


source


The main problem is that you are taking 1 DataTable and trying to split it into 2 Linked Data Tables. The best way would be to split the table so that you have an Employee Table and an Address table associated with them through EmployeeId or something.

Then you fetch information into two different tables, link and insert them accordingly:



DataSet empList = new DataSet("EmployeeDetails");
DataTable employee = Employees;
DataTable address = Addresses;

empList.Add(employee);
empList.Add(address);

DataRelation relEmpAdd = new DataRelation
(
    "relEmpAdd"
    ,employee.Columns["Id"]
    ,address.Columns["EmployeeId"]
);
relEmpAdd.Nested = true;

ds.Relations.Add(relEmpAdd);

      

0


source







All Articles