1

我想创建两个数据表。

第一个应该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();
        }

有人可以帮我吗?

4

2 回答 2

0

您可以使用具有许多 DataSet 的 DataSet 看到这个:

在包含多个 DataTable 对象的DataSet中,您可以使用 DataRelation 对象将一个表与另一个表关联,在表中导航,并从相关表返回子行或父行。

于 2013-07-08T10:21:27.037 回答
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 };
    }
于 2013-07-29T10:21:27.317 回答