我想创建两个数据表。
首先应该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>
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();
}
任何人都可以帮我这个吗?
答案 0 :(得分:0)
您可以使用包含许多DataSet的DataSet,请参阅:
在包含多个DataTable对象的DataSet中,您可以使用 DataRelation对象将一个表与另一个表相关联,以进行导航 通过表,并从相关的返回子行或父行 表
答案 1 :(得分:0)
试试这段代码,它对我有用
public DataTable[] ParseXML()
{
DataTable dtEmployeeDetails = new DataTable("Employees");
DataColumn column = new DataColumn("EmpID", typeof(Int32));
column.AutoIncrement = true;
dtEmployeeDetails.Columns.Add(column);
dtEmployeeDetails.Columns.Add("FirstName", typeof(String));
dtEmployeeDetails.Columns.Add("LastName", typeof(String));
dtEmployeeDetails.Columns.Add("Location", typeof(String));
DataTable dtOrderDetails = new DataTable("Orders");
DataColumn column2 = new DataColumn("ID", typeof(Int32));
column2.AutoIncrement = true;
dtOrderDetails.Columns.Add(column2);
dtOrderDetails.Columns.Add("OrderId", typeof(String));
dtOrderDetails.Columns.Add("Details", typeof(String));
XElement elementXML = XElement.Load(@"C:\Users\Guest\Documents\Visual Studio 2012\Projects\ConsoleApplication\Orders.xml");
IEnumerable<XElement> elem = elementXML.Elements();
foreach (var employees in elem)
{
string firstName = employees.Element("firstname").Value;
string lastname = employees.Element("lastname").Value;
string location = employees.Element("location").Value;
dtEmployeeDetails.Rows.Add(null,firstName, lastname, location);
//var orders = from order in elementXML.Descendants("order")
//select order;
foreach (var order in employees.Descendants("order"))
{
string orderId = order.Element("orderId").Value;
string details = order.Element("details").Value;
dtOrderDetails.Rows.Add(null, orderId, details);
}
}
return new DataTable[2] { dtEmployeeDetails,dtOrderDetails };
}