我想创建两个数据表。
第一个应该empid,firstname,lastname,location
和
第二个表应该给出empId,OrderId,details
这个 XML 数据源。
我试过了,但它不起作用。
我的代码是:
<?xml version="1.0" encoding="utf-8" ?>
<details>
<employee>
<firstname>Amit</firstname>
<lastname>Jain</lastname>
<location>Mumbai</location>
<order>
<orderId>01254</orderId>
<details>Aaa,bbbb</details>
</order>
<order>
<orderId>01255</orderId>
<details>Aaa,bbbb</details>
</order>
</employee>
<employee>
<firstname>User</firstname>
<lastname>1</lastname>
<location>Delhi</location>
<order>
<orderId>01256</orderId>
<details>Aaa,bbbb</details>
</order>
<order>
<orderId>01257</orderId>
<details>Aaa,bbbb</details>
</order>
</employee>
<employee>
<firstname>User</firstname>
<lastname>2</lastname>
<location>Bangalore</location>
<order>
<orderId>01258</orderId>
<details>Aaa,bbbb</details>
</order>
<order>
<orderId>01259</orderId>
<details>Aaa,bbbb</details>
</order>
</employee>
</details>
C# 代码
protected void btnReadXmlFile_Click(object sender, EventArgs e)
{
string filePath = Server.MapPath("~/example.xml");
//Employee Must match with the element name in
//your file
DataTable dt = new DataTable("employee");
//Add Columns in datatable
//Column names must match XML File nodes
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.ColumnName = "EmpID";
dt.Columns.Add(column);
dt.Columns.Add("firstname", typeof(System.String));
dt.Columns.Add("lastname", typeof(System.String));
dt.Columns.Add("location", typeof(System.String));
//Read XML File And Display Data in GridView
dt.ReadXml(filePath);
GridView1.DataSource = dt;
GridView1.DataBind();
//
DataTable dt2 = new DataTable("order");
//Add Columns in datatable
//Column names must match XML File nodes
DataColumn column2 = new DataColumn();
column2.DataType = System.Type.GetType("System.Int32");
column2.AutoIncrement = true;
column2.ColumnName = "ID";
dt2.Columns.Add(column2);
dt2.Columns.Add("orderId", typeof(System.String));
dt2.Columns.Add("details", typeof(System.String));
//Read XML File And Display Data in GridView
dt.ReadXml(filePath);
GridView2.DataSource = dt2;
GridView2.DataBind();
}
有人可以帮我吗?