【问题标题】:Create Two Data Table From XML Data Source With Relationship从具有关系的 XML 数据源创建两个数据表
【发布时间】:2013-07-08 10:19:32
【问题描述】:

我要创建两个数据表。

第一个应该empid,firstname,lastname,location,和

第二个表应该通过这个 XML 数据源提供empId,OrderId,details

我试过了,但它不起作用。

我的代码是:

<?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();
        }

有人可以帮我吗?

【问题讨论】:

    标签: c# asp.net .net xml


    【解决方案1】:

    试试这个代码,它对我有用

    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 };
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用具有许多 DataSet 的 DataSet,请参阅:

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

      【讨论】:

        猜你喜欢
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-16
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        相关资源
        最近更新 更多