Thank you for reading my blog.I received 40 emails yesterday.
Here is how to bind xml data to GridView or DataGrid:
Download aspnetbooks.xml source code here
Step 1:
-Add reference to System.Data and System.XML
In VB.NET :
imports System.Data
imports System.Xml
In C# :
using System.Data;
using System.Xml;
Step2:
-Drop a GridView or DataGrid on designer surface
-Double click on designer for Page_Load event
Step3:Add this code inside Page_Load
In VB.NET :
Dim ds As New DataSet
Dim XMLMode As New System.Data.XmlReadMode
ds.ReadXml(GetXMLPath("Demos/aspnetbooks.xml"))
Me.GridView1.DataSource = ds
Me.GridView1.DataBind()
I programmed GetXMLPath function which is:
Protected Function GetXMLPath(ByVal xmlpath As String) As String
Return Request.PhysicalApplicationPath & xmlpath
End Function
In C#:
DataSet ds=new DataSet();
System.Data.XmlReadMode XMLMode=new System.Data.XmlReadMode();
ds.ReadXml(GetXMLPath("Demos/aspnetbooks.xml"))
this.GridView1.DataSource = ds
this.GridView1.DataBind()
for the function:
protected string GetXMLPath(string xmlpath)
{
return Request.PhysicalApplicationPath + xmlpath
}
Note + sign instead of & inside GetXMLPath method(C# uses + while VB.NET uses &)
The code inside VS should look like this:
The output should look like this:
Thank you for reading.
Any question or if you need private online training please email me at
johnstalley@live.com
Talley Ouro
Raleigh,NC
No comments:
Post a Comment