Binding XML data to DataGrid (VB.net) : DataGrid « XML « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<script runat=server>
   private i, j as integer
   sub Page_Load(Sender as Object, e as EventArgs)
      dim xmldoc as new XmlDataDocument()
         
         xmldoc.DataSet.ReadXml(Server.MapPath("Data.xml"))
         
         DataGrid1.DataSource = xmldoc.DataSet
         DataGrid1.DataMember = xmldoc.DataSet.Tables(0).TableName
         DataGrid2.DataSource = xmldoc.DataSet
         DataGrid2.DataMember = xmldoc.DataSet.Tables(1).TableName
         
         DataGrid1.DataBind()
         DataGrid2.DataBind()
         
         For i = 0 To xmldoc.DataSet.Tables.Count - 1
            output.Text += "TableName = """ & _
               xmldoc.DataSet.Tables(i).TableName & """<br>"
            output.Text += "Columns count " & _
               "= " & xmldoc.DataSet.Tables(i).Columns.Count. _
                  ToString() & "<br>"

            For j = 0 To xmldoc.DataSet.Tables(i).Columns.Count-1
               output.Text += "ColumnName = """ & xmldoc.DataSet. _
                 Tables(i).Columns(j).ColumnName & """, " & _
                 "type = " & xmldoc.DataSet.Tables(i). _
                 Columns(j).DataType.ToString() & "<br>"
            Next
         Next
         
         output.Text += ""
       
   end sub
</script>

<html><body>
   <asp:Label id="output" runat="server" />
   
   <asp:DataGrid id="DataGrid1" runat="server" 
         BorderColor="black" 
         GridLines="Vertical" 
         ItemStyle-BackColor="#ffffff"
         AlternatingItemStyle-Backcolor="#cccccc" />
   
   <asp:DataGrid id="DataGrid2" runat="server" 
         BorderColor="black" 
         GridLines="Vertical" 
         AlternatingItemStyle-Backcolor="#cccccc" />
</body>
</html>

File: Data.xml


<?xml version="1.0"?>
<bookstore>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
  <book genre="asdf">
    <title>asdf</title>
    <author>
      <first-name>asdf</first-name>
      <last-name>asdf</last-name>
    </author>
    <price>asdf</price>
  </book>
</bookstore>








25.1.DataGrid
25.1.1.Use DataGrid to edit data in XML (VB.net)
25.1.2.Binding XML data to DataGrid (VB.net)
25.1.3.Save data from DataGrid to XML