Results 1 to 6 of 6

Thread: Reading XML files

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    35

    Reading XML files

    I have an XML file (see attached) that has numerous different sections/categories.

    What I'm looking for is advice on how to pick out the different levels/categories and display them one after the other, with the click of a button obviously. Much like you would/can with a recordset using ADO.

    I'll be using vb 2008.
    Attached Files Attached Files

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Reading XML files

    Hey,

    There are a number of ways that you can go about doing this.

    For instance you can use the .SelectSingleNode and .SelectNodes , methods on the XmlDocument class, which take as an input an XPath query. If you search the forums for either of those methods you will find lots of examples, and you can follow the link in my signature for finding out about XPath.

    Another alternative would be to use LINQ. I don't know too much about this, but again, if you search the forum you will find examples of using LINQ with XML.

    Hope this helps!!

    Gary

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    35

    Re: Reading XML files

    Thanks for that.

    Just a question though, one of the nodes I'm looking at has quotes (double) in it, around cate and gui (see code below)
    Code:
    <integer id="cate" value="gui " />
    Do I need to include these double quotes in my code or not? Below is my vb code but it produces an error '//integer id="cate" value="gui "' has an invalid token', any ideas why, and what does it mean?
    Code:
    Dim nod As XmlNode = xd.SelectSingleNode("//integer id=""cate"" value=""gui """)

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Reading XML files

    Hey,

    Did you read the link that I pointed you to?

    What you are describing are the attributes on the XML Node, and using XPath, they are accessed in a different way, i.e the XPath query that you would need, would be something like:

    Code:
    /properties/list/record/integer[@id="cate"]
    Hope that helps!!

    Gary

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2008
    Posts
    35

    Re: Reading XML files

    Would that method return all attribute matching the xpath query, or just the first one?

    If it is just the first one what method do i need to use to return all matching attributes? And also how do i perform a loop in the xml to count all matching attributes?

    Another thing, what about the value="gui " part of the query? would i just add & value[@id="gui " to your example, ie.
    Code:
    /properties/list/record/integer[@id="cate"] & value[@id="gui "]
    Cheers for your help.
    Last edited by conners; Apr 8th, 2009 at 05:09 AM.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Reading XML files

    Hey,

    If you haven't done so already, download the XpathBuilder from the link in my signature, it makes answering that question very easy

    Name:  XPathBuilder.jpg
Views: 116
Size:  240.4 KB

    Rather than & you would use "and" as in:

    /properties/list/record/integer[@id="cate" and @id="gui"]

    This XPath doesn't work in XPathBuilder, I can't remember the reason why is doesn't but as per the XPath Standard it should work, so give it a try in your code.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width