قراءة ملف من نوع XML والتخزين داخل Data Table

قراءة ملف من نوع XML والتخزين داخل Data Table



في البداية , نفتح مشروع جديد ونضع بداخله ملف XML كما في الصورة .


 محتويات ملف XML


<!--?xml version="1.0" standalone="yes"?-->
<details>
  <employee>
    <firstname>muhammed</firstname>
    <lastname>Yasser</lastname>
    <location>Palestine</location>
  </employee>
  <employee>
    <firstname>samer</firstname>
    <lastname>Zaki </lastname>
    <location>Palestine</location>
  </employee>
  <employee>
    <firstname>Khalil</firstname>
    <lastname>Khaled</lastname>
    <location>Palestine</location>
  </employee>
</details>



 كود البرمجة لتخزين XML في Data Table , يتم تنفيذه بعد الضغط على button.

 protected void Button1_Click(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("~/Employee.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
        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();
    }


يمكنك تحميل الكود بشكل كامل من هنا


شارك الموضوع
تعليقات