XML - traversing in VB

A

Asad

Hi, I have an XML file that looks something like this:

- <Geo>
<state>Alabama</state>
<capital>Montgomery</capital>
<abbrev>AL</abbrev>
<timezone>C</timezone>
</Geo>
- <Geo>
<state>Alaska</state>
<capital>Juneau</capital>
<abbrev>AK</abbrev>
<timezone>A</timezone>
</Geo>

so on and so forth. I want to write a function in VB, where if the
function is sent AL (abbreviation) it returns the state name or some
other information in that group. I worked with XML long time ago, and
can't recall right now how to do this. Can someone refresh my memory?
BTW, I want to use XMLTextReader to do this.

Thanks.
 
M

Matt Berther

Hello (e-mail address removed),
Hi, I have an XML file that looks something like this:

- <Geo>
<state>Alabama</state>
<capital>Montgomery</capital>
<abbrev>AL</abbrev>
<timezone>C</timezone>
</Geo>
- <Geo>
<state>Alaska</state>
<capital>Juneau</capital>
<abbrev>AK</abbrev>
<timezone>A</timezone>
</Geo>
so on and so forth. I want to write a function in VB, where if the
function is sent AL (abbreviation) it returns the state name or some
other information in that group. I worked with XML long time ago, and
can't recall right now how to do this. Can someone refresh my memory?
BTW, I want to use XMLTextReader to do this.

I dont think that you'll be able to use an XmlTextReader to accomplish this, since the XmlTextReader is a forward only cursor through the Xml.

I would think that the best way to accomplish this would be to load the document in an XmlDocument and then use an XPATH query to get the appropriate Geo node.

Example (Excuse the C# code. My VB.NET is rusty):
XmlNode node = document.SelectSingleNode("/Geo[Abbrev = 'AL']");
string stateName = node["state"].InnerText;

Since this Xml is more than likely readonly, you could also look at using the XPathNavigator class to increase performance a little.

HTH!
 

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

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top