Saturday, March 24, 2012

Populate dropdown list with XML

How can I fill a dropdown list with XML data. Simplest way please. I would like to user server.mappath to get the data from the file. Is it possible to use a recordset

I need to populate the ID and the value of the record.

Code sample appreciated...thanksPlease see below code, that helps for your question.

Here is the simple XML file, I am using to bind to dropdownlist and name is as Xmlfile.xml


<?xml version="1.0" encoding="utf-8" ?>
<DataList>
<Student>
<Name>Creator</Name >
<Roll>101</Roll >
</Student >
<Student>
<Name>Maintainer</Name >
<Roll>102</Roll >
</Student
</DataList>

and I am using this file to bind with Dropdownlost in page load event. Assuming DropDownList server control name is DropDownList1


Dim Ds As New System.Data.DataSet
Ds.ReadXml("D:\WebSites\VS2005First\XMLFile.xml")

DropDownList1.DataSource = Ds.Tables(0).DefaultView
DropDownList1.DataTextField = "Roll"
DropDownList1.DataValueField = "Name"
DropDownList1.DataBind()

Hope this helps you!
ok that is pretty easy,fist of all u need to populate ur xml data to your datagrid then bind it to ur dropdownlist.

xml file
<root>
<userName>Bob</userName>
<userID>1</userID>
</root
vb.net
'read your data from your datagrid
'assuming ur xml is your root directory
'

Dim ds As DataSet
ds.ReadXml(Server.MapPath("file.xml"))
'bind your dataset to your dropdown list
DropDownList1.DataSource = ds
'assign the element of ur xml to the ddl
DropDownList1.DataTextField = "userName"
DropDownList1.DataValueField = "userID"
DropDownList1.DataBind()

hope that help.Happy coding
Thank you all for replying - I will try the suggestions given

0 comments:

Post a Comment