473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 18 '05 #1
3 1133
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 18 '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 18 '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 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nut Shell | last post by:
I needed to parse a csv file for my project. Could not find any so I put together this code. It works for general cases. Use it and let me know. Just Cut and paste the 2 functions in a new Form...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
8
by: Uwe Mayer | last post by:
Hi, can someone provide me with a running example for subclassing QWidget (or something similarly simple) in C++ and then creating SIP (4.x+) bindings for in for Python (2.3+)? I am looking...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
3
by: VB Programmer | last post by:
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...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
0
by: Ming Zhu | last post by:
Hi, all, I'm new to .NET and ASP.NET. I only use .NET Framework 1.0. I'm reading the book "C# developer's guide to ASP.NET". But I find there is no place to download the code used inside this...
7
by: Miro | last post by:
Hi, ( using vb.net 2003 ) Im not quite sure what to look for in the help / online for this. Example: You have 2 seperate .exe files running ( both written in vb.net ) Is there a way to...
0
by: JohnLucas | last post by:
Hi all, I have just started working with the XML::Simple module to parse an XML file. I'm trying to pull some values from the file that I need in another program. The problem is that the XML...
2
by: friend.blah | last post by:
can any one give a simple example for implementing mutex. I have 3 threads (taking different input files )enter into the same function at different times... The 3 threads need to exit/leave the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.