如何从数据表或数据集创建嵌套XML而无需循环? [英] How to create nested XML from datatable or dataset without looping?
问题描述
我想从DataTable创建无循环的嵌套XML。
I want to create nested XML from DataTable without loop.
数据表:员工
我尝试了以下代码段
DataSet ds=new DataSet ("EmployeeDetails");
DataTable dtConvert = datatable to be converted
ds.Tables.Add(dtConvert);
ds.WriteXml("sample.txt");
并获得了如下所示的XML,
and got the XML which 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>
但是我想要的XML格式是
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 please guide me to do this in a best way?
推荐答案
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
创建xml
我在这里要做的是创建2个表雇员
和地址
。 地址
有4列 ID
-引用 Employee
主键,州
,城市
,国家
。 Employee
有2列 Name
, ID
-主键。然后将这些表添加到数据集 ds
。最后,从 ds
创建xml。
What I'm trying to do here is creating 2 tables Employee
and Address
. Address
has 4 columns ID
- foreign key that refer to Employee
primary key, State
, City
, Country
. Employee
has 2 columns Name
, ID
- primary key. Then add those tables to dataset ds
. Finally, create xml from ds
.
P / S:我用纯文本形式编写(不使用IDE,因为这台计算机没有任何IDE :(,因此,如果拼写错误或语法错误,请让我知道)
P/S: I write this in pure text (without using IDE because this computer doesn't has any IDE :(, so if any error on typo or syntax, please let me know)
更新我更新了代码,现在结果XML如下所示:
UPDATE I updated the code, now the result XML will look 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>
我从上一次的关系中弄错了时间,它应该是 address
地址中的外键,它引用 employee
主键(在这种情况下,我是用户列 ID
)
I made a mistake with the relationship from last time, it should be a foreign key in address
that refer to employee
primary key (in this case, I user column ID
)
UPDATE2 以从地址
中排除ID,请在调用 ds.WriteXML( test.xml)$ c之前添加此行$ c>
UPDATE2 to exclude ID from Address
, add this line right before you call ds.WriteXML("test.xml")
ds.Tables["Address"].Columns["ID"].ColumnMapping = MappingType.Hidden;
您还可以添加以下行以从员工中排除ID:
You can also add following line to exclude ID from employee as well:
ds.Tables["Employee"].Columns["ID"].ColumnMapping = MappingType.Hidden;
这篇关于如何从数据表或数据集创建嵌套XML而无需循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!