Groups | Blog | Home
all groups > asp.net building controls > september 2003 >

asp.net building controls : displaying XML data in datagrid column



Nedu N
9/17/2003 3:52:52 PM
i want to display actual XML data (with tags & data) from a table's colum
(XML data) in a datagrid's column...

by default it shows only yhe content of the XML but not the tags...anyone
have an answer for this...

i don't want to bind the data to a text box/lable in order to overcome
this..

lukezhan NO[at]SPAM online.microsoft.com
9/18/2003 2:50:32 AM
Hi Nedu,

How did you bind the XML data to the datagrid? Normally, we can first
convert a XML file to a DataSet, and then bind the datagird to the dataset.
For example, we have a XML file "XMLFile1.xml":

<?xml version="1.0" encoding="utf-8" ?>
<Books>
<book>
<id> 1</id>
<name> One</name>
</book>

<book>
<id> 2</id>
<name> Two</name>
</book>

<book>
<id> 3</id>
<name> Three</name>
</book>

</Books>

In an ASPNET application, we can use following code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load


Dim ds As New DataSet

ds.ReadXml(Server.MapPath("\webapplication1\xmlfile1.xml"))

DataGrid1.DataSource = ds.Tables(0)

DataGrid1.DataBind()

End Sub

And then the XML data with be shown the datagrid. As you seen, the tag <id>
and <name> will be used as column name in the datagrid.

Luke
Microsoft Online Partner Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Henrik Grotle
9/18/2003 11:21:25 AM
[quoted text, click to view]

You can use Server.HtmlEncode to achieve the required result:

<ItemTemplate><%# Server.HtmlEncode(DataBinder.Eval(Container.DataItem,
"xml"), 0) %></ItemTemplate>

--
Henrik Grotle

AddThis Social Bookmark Button