Results 1 to 5 of 5

Thread: Reading An XML File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Reading An XML File

    Hi GUys,

    i have my .xml files written like:

    vb.net Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <hiddenFields>
    3.    <hiddenFieldsIDNumber0>sta</hiddenFieldsIDNumber0>
    4.    <hiddenFieldsIDNumber1>mand</hiddenFieldsIDNumber1>
    5. </hiddenFields>

    I'm trying to read the values but i'm not having much luck, i have this:

    vb.net Code:
    1. Function functionLoadHiddenFields(ByVal fileLocation As String)
    2.  
    3.         Try
    4.  
    5.             '// Check if the file exists
    6.             If File.Exists(fileLocation) Then
    7.  
    8.                 '// Load the xml document
    9.                 Dim XMLDoc As New Xml.XmlDocument
    10.  
    11.                 '// Find and load the XML file
    12.                 XMLDoc.Load(fileLocation)
    13.  
    14.                 '// Tell where to look for the data we need
    15.                 Dim XMLItem As Xml.XmlNode = XMLDoc.SelectSingleNode("hiddenFields/")
    16.  
    17.             Else
    18.                 '// oops, the file is missing or wasn't written!
    19.                 MessageBox.Show("A file is missing that we need to use!, please restart the program", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    20.             End If
    21.  
    22.         Catch ex As Exception
    23.  
    24.             Return False
    25.  
    26.         End Try
    27.  
    28.     End Function

    i'm not to sure how to get the individual values, any help would be great!

    thanks guys

    Graham
    Last edited by graham23s; Aug 26th, 2009 at 07:28 AM.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Reading An XML File

    To read an xml file, you want to use the XmlReader class instead of the XmlDocument class.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Reading An XML File

    You can find a entry in the VB.NET codebank about reader/writing xml.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Reading An XML File

    look at the .hasChildren property as well as the .firstChild one...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Reading An XML File

    Here are a couple XPATH samples
    Code:
    'get hiddenFields node
    Dim xmlItem = XMLDoc.SelectSingleNode("hiddenFields")
    
    'get hiddenFields child nodes
    For Each xmlItem In XMLDoc.SelectNodes("hiddenFields/*")
    
    Next
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

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