如何在不循环的情况下从数据表或数据集创建嵌套XML?

时间:2014-01-05 15:04:39

标签: c# xml datatable dataset

我想在没有循环的情况下从DataTable创建嵌套XML。

DataTable:员工 enter image description here

我曾尝试过以下代码段

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

并获得了类似的XML,

<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>

但我想要的XML格式是

<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>

有人可以指导我以最好的方式做到这一点吗?

2 个答案:

答案 0 :(得分:3)

        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

我在这里要做的是创建2个表EmployeeAddressAddress有4列ID - 引用Employee主键的外键,StateCityCountryEmployee有2列NameID - 主键。然后将这些表添加到数据集ds。最后,从ds创建xml。

P / S:我用纯文本写这个(不使用IDE,因为这台电脑没有任何IDE :(,如果有任何关于拼写错误或语法的错误,请告诉我)

UPDATE 我更新了代码,现在结果XML将如下所示:

<?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>

我上次与关系犯了一个错误,它应该是address中引用employee主键的外键(在这种情况下,我是用户列ID

UPDATE2 要从Address中排除ID,请在致电ds.WriteXML("test.xml")之前添加此行

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

您还可以添加以下行以从员工中排除ID:

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

答案 1 :(得分:0)

您遇到的主要问题是您正在使用1个DataTable并尝试将其分解为2个相关数据表。最好的方法是打破你的表,这样你就有了一个员工表和一个地址表,通过EmployeeId或其他东西将它们相关联。

然后,将信息拉入两个不同的表,关联,并相应地嵌套它们:

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);