XML - traversing in VB

Discussion in 'ASP .Net' started by Asad, Apr 16, 2004.

  1. Asad

    Asad Guest

    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.
     
    Asad, Apr 16, 2004
    #1
    1. Advertising

  2. Asad

    Matt Berther Guest

    Hello ,

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

    --
    Matt Berther
    http://www.mattberther.com
     
    Matt Berther, Apr 16, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steven
    Replies:
    6
    Views:
    917
    steven
    Aug 27, 2003
  2. Replies:
    4
    Views:
    676
  3. thomson
    Replies:
    0
    Views:
    321
    thomson
    Dec 27, 2005
  4. M
    Replies:
    0
    Views:
    368
  5. Michaelp

    Help with traversing an XML structure

    Michaelp, Aug 6, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    109
    Michaelp
    Aug 6, 2009
Loading...

Share This Page