469,620 Members | 1,857 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,620 developers. It's quick & easy.

Example needed: simple XML file and parsing

A dropdownlist in my ASP.NET webform needs to be populated from values taken
from an XML file. Can someone provide a (simple) sample XML file and a
parsing routine using VB.NET?

The values could be something like:
Fruits
Vegetables
Meats
Dairy

Thanks in advance!
Nov 12 '05 #1
3 4373
How about something like this:

Dim dstDataSet As New DataSet
dstDataSet.ReadXml(Server.MapPath(".") & "\Items.xml")
With DropDownList1
.DataSource = dstDataSet
.DataValueField = "id"
.DataTextField = "name"
.DataBind()
End With

Then create an Items.xml file that contains:

<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>
<id>1</id>
<name>Fruits</name>
</item>
<item>
<id>2</id>
<name>Vegetables</name>
</item>
</items>
"VB Programmer" <gr*********@go-intech.com> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
A dropdownlist in my ASP.NET webform needs to be populated from values taken from an XML file. Can someone provide a (simple) sample XML file and a
parsing routine using VB.NET?

The values could be something like:
Fruits
Vegetables
Meats
Dairy

Thanks in advance!

Nov 12 '05 #2
Thanks Dan! I appreciate your example.

"Dan Cooper" <da********@tiscali.co.uk> wrote in message
news:OY*************@tk2msftngp13.phx.gbl...
How about something like this:

Dim dstDataSet As New DataSet
dstDataSet.ReadXml(Server.MapPath(".") & "\Items.xml")
With DropDownList1
.DataSource = dstDataSet
.DataValueField = "id"
.DataTextField = "name"
.DataBind()
End With

Then create an Items.xml file that contains:

<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>
<id>1</id>
<name>Fruits</name>
</item>
<item>
<id>2</id>
<name>Vegetables</name>
</item>
</items>
"VB Programmer" <gr*********@go-intech.com> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
A dropdownlist in my ASP.NET webform needs to be populated from values

taken
from an XML file. Can someone provide a (simple) sample XML file and a
parsing routine using VB.NET?

The values could be something like:
Fruits
Vegetables
Meats
Dairy

Thanks in advance!


Nov 12 '05 #3
VB Programmer wrote:
A dropdownlist in my ASP.NET webform needs to be populated from values taken
from an XML file. Can someone provide a (simple) sample XML file and a
parsing routine using VB.NET?

The values could be something like:
Fruits
Vegetables
Meats
Dairy

Thanks in advance!


Check out the System.Xml.XmlDocument class, it has load methods, etc. to
load in the doc or string. Then you use XPath to query and select the
nodes with your values (XmlDocument.SelectNodes I believe). Here's a
quick example snippet (sorry only example I could find quickly):

http://www.net-language.com/CodeExample.aspx?i=185

If it's decent XML, all of your values should be selectable at once (one
method call like above). Then you can bind the resulting XmlNodeList to
your dropdown. Each Container.DataItem will then be an XmlNode.

In the dropdown, set the DataTextField to the name of whatever property
of the XmlNode is the one to display on the screen (ie. "Text") and the
DataValueField as well (probably the same unless you have some other
attribute, etc. that is used for the value).
--
Craig Deelsnyder
Microsoft MVP - ASP.NET
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ville Vainio | last post: by
8 posts views Thread by Uwe Mayer | last post: by
67 posts views Thread by Steven T. Hatton | last post: by
10 posts views Thread by Bart Goeman | last post: by
reply views Thread by Ming Zhu | last post: by
3 posts views Thread by VB Programmer | last post: by
7 posts views Thread by Miro | last post: by
2 posts views Thread by friend.blah | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.