Example needed: simple XML file and parsing

V

VB Programmer

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!
 
D

Dan Cooper

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>
 
V

VB Programmer

Thanks Dan! I appreciate your example.

Dan Cooper said:
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>
 
C

Craig Deelsnyder

VB said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top