[英]How to create nested XML from datatable or dataset without looping?
我想在沒有循環的情況下從DataTable創建嵌套XML。
DataTable:員工
我曾嘗試過以下代碼段
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>
誰能指導我以最好的方式做到這一點?
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個表Employee
和Address
。 Address
有4列ID
- 指向Employee
主鍵, State
, City
, Country
外鍵。 Employee
有2列Name
, ID
- 主鍵。 然后將這些表添加到數據集ds
。 最后,從ds
創建xml。
P / S:我用純文本寫這個(不使用IDE,因為這台計算機沒有任何IDE :(,如果有任何關於拼寫錯誤或語法的錯誤,請告訴我)
更新我更新了代碼,現在結果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>
我上次與關系犯了一個錯誤,它應該是一個引用employee
主鍵的address
中的外鍵(在這種情況下,我是用戶列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個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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.