|
Binding
XML To a Datagrid
During
web development, you might come across many situations where you need to bind
XML to a DataGrid. There are many ways to do this, for example we can load
the XML into a dataset and then bind the dataset to datagrid. You
might think that for just binding XML, why i need to create a dataset
which is an overhead. In that case how i can directly bind XML to
datagrid. In this article you are going to see how you can bind XML to a
datagrid directly and also using dataset.
Using DataSet
Before you see how you can bind XML
to datagrid directly,i am going to explain how you can bind the XML to datagrid
using dataset. DataSet has a method "readXML" which reads XML Schema
and data into the Dataset. Using this method you can load XML in to
Dataset, then this dataset can be set as Datasource for DataGrid. Consider you
have datagrid like this,
<asp:datagrid
id="DataGrid1" runat="server" BorderColor="black"
HeaderStyle-BackColor="#aaaadd" HeaderStyle-CssClass="tableHeader"
ItemStyle-CssClass="tableItem" AllowPaging="False" width="700px"
AutoGenerateColumns=False>
<Columns>
<asp:TemplateColumn headertext="Product ID" >
<ItemTemplate
>
<asp:TextBox style= "width:100px;" id="ProductID"
runat=
"server"
Text='<%# Container.DataItem("ProductID")
%>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:400px;" id="ProductName" runat="server"
Text='<%# Container.DataItem("Name") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Product Price">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="ProductPrice"
runat="server"
Text='<%# Container.DataItem("Price") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Quantity">
<ItemTemplate>
<asp:TextBox style="width:100px;" id="Quantity" runat="server"
Text='<%# Container.DataItem("Quantity") %>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
In
Page_Load event handler, you need to bind this datagrid with the XML. In this
example you have xml as text file in the same directory. So loading
that xml in to dataset and binding that to datagrid will be like this,
Private
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
If
Not IsPostBack Then
Dim
ds As New DataSet
oDataSet.ReadXml(Server.MapPath("product.xml"))
DataGrid1.DataSource = oDataSet
DataGrid1.DataBind()
End
If
End Sub
Binding XML Directly to datagrid.
Binding XML to Datagrid using dataset is
easiest and it will work in all the cases. But you might think that, i
just want to bind the values from XML to a Datagrid to just display that data.
In that case, why I want to use dataset it might be a overhead. To avoid this
you need to bind the datagrid directly. When i read
Kirk Allen Evans' Blog,
i got an idea for how to bind the XML directly to datagrid. I just
modified that approach for my needs. Here I am going to explain that
approach only.
In this approach you need to use
XMLDataDocument or XMLDocument to load the xml instead of Dataset.
You can set any object which implements IEnumerable as datasource to DataGrid.
Since XMLDocument doesnt implement IEnumberable to DataGrid. But
if Nodes collection of XMLDocument is collection object which does
implement IEnumberable, so we can set this as datasource for
datagrid. Consider similar datagrid like the above method,
<asp:datagrid id="DataGrid1"
runat="server" BorderColor="black" HeaderStyle-BackColor="#aaaadd"
HeaderStyle-CssClass="tableHeader" ItemStyle-CssClass="tableItem"
AllowPaging="False" width="700px" AutoGenerateColumns=False>
<Columns>
<asp:TemplateColumn headertext="Product ID" >
<ItemTemplate
>
<asp:TextBox style=
"width:100px;" id="ProductID" runat=
"server"
Text='<%#
CType(Container.DataItem,System.XML.XMLNode)("ProductID").InnerText%>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Product Name">
<ItemTemplate>
<asp:TextBox style="width:400px;" id="ProductName"
runat="server"
Text='<%#
CType(Container.DataItem,System.XML.XMLNode)("Name").InnerText%>'
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Product Price">
<ItemTemplate>
<asp:TextBox
style="width:100px;" id="ProductPrice" runat="server"
Text='<%#
CType(Container.DataItem,System.XML.XMLNode)("Price").InnerText%>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
headertext="Quantity">
<ItemTemplate>
<asp:TextBox
style="width:100px;" id="Quantity" runat="server"
Text='<%#
CType(Container.DataItem,System.XML.XMLNode)("Quantity").InnerText%>' >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
Only change is, converting
Container.DataItem as XMLNode and then finding out each item's value.
Since you know if you bind Nodes as datasource for DataGrid,
container.DataItem will be each node in Nodes collection. Then from Node you
can each item value using XMLDocument approach. So this way you can bind XML
Directly to Dataset, Page_Load event handler for this approach will be like
this.
Private
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
If Not IsPostBack
Then
Dim oXML As New XmlDataDocument
oXML.Load(Server.MapPath("product.xml"))
DataGrid1.DataSource = oXML.SelectNodes("DataSet/Product")
DataGrid1.DataBind()
End If
End Sub
Product.XML file is having following
data,
<?xml version="1.0"
standalone="yes"?>
<DataSet>
<Product>
<Name>Onida TV</Name>
<ProductID>1</ProductID>
<Price>12000</Price>
<Quantity>2</Quantity>
</Product>
<Product>
<Name>Samsung TV</Name>
<ProductID>2</ProductID>
<Price>12000</Price>
<Quantity>2</Quantity>
</Product>
<Product>
<Name>LG TV</Name>
<ProductID>3</ProductID>
<Price>12000</Price>
<Quantity>2</Quantity>
</Product>
</DataSet>
Conclusion
In this article, you have seen two
methods for binding XML to a datagrid. Depending upon your requirement you can
choose which method to use. Binding XML directly to DataGrid method can not be
used in all the cases, so if you feel that this approach can be implemented in
your application. Then implement this method itself. This method might your
improve application performance when you compare with other approach of
binding XML to a DataGrid using Dataset.
|