2013-09-23 25 views
0

我有以下數據到XML文件中,我想在網格中顯示。 我將通過數據表將此數據綁定到網格。 我對讀取節點並將行添加到數據表感到困惑。 我該怎麼辦?在VB.NET中綁定XML到網格

<?xml version="1.0" encoding="utf-8"?> 
<Employees> 
    <Employee> 
    <ID>1</ID> 
    <FirstName>Prakash</FirstName> 
    <LastName>Rangan</LastName> 
    <Salary>70000</Salary> 
    </Employee> 
    <Employee> 
    <ID>5</ID> 
    <FirstName>Norah</FirstName> 
    <LastName>Miller</LastName> 
    <Salary>21000</Salary> 
    </Employee> 
    <Employee> 
    <ID>17</ID> 
    <FirstName>Cecil</FirstName> 
    <LastName>Walker</LastName> 
    <Salary>60000</Salary> 
    </Employee> 
</Employees> 

回答

0
Protected Sub btnReadXmlFile_Click(sender As Object, e As EventArgs) 

    Dim filePath As String = Server.MapPath("~/Employees.xml") 

    'Employee Must match with the element name in 

    'your file 

    Dim dt As New DataTable("Employee") 



    'Add Columns in datatable 

    'Column names must match XML File nodes 

    dt.Columns.Add("ID", GetType(System.Integer)) 

    dt.Columns.Add("FirstName", GetType(System.String)) 

    dt.Columns.Add("LastName", GetType(System.String)) 

    dt.Columns.Add("Salary", GetType(System.Integer)) 


    'Read XML File And Display Data in GridView 

    dt.ReadXml(filePath) 

    GridView1.DataSource = dt 

    GridView1.DataBind() 

End Sub