0

我在 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>
4

1 回答 1

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
于 2013-09-23T11:07:44.650 回答