Connecting Tech Pros Worldwide Help | Site Map

Reading XML nodes

Newbie
 
Join Date: Dec 2007
Posts: 3
#1: Sep 22 '09
I have a XML file that stores information about an application I am trying to read and it looks like this:

Expand|Select|Wrap|Line Numbers
  1. <Data>
         <Login UserName="Username" Password="Password"/>
  2.      <Application Name="AppName1" Key="Key1" Owner="Owner1"/>
  3.      <Application Name="AppName2" Key="Key2" Owner="Owner2"/>
  4.      <Application Name="AppName4" Key="Key3" Owner="Owner3"/>
  5.      <Owner Name="Owner1"/>
  6.      <Owner Name="Owner2"/>
  7.      <Owner Name="Owner3"/>     </Data>
After all of the applications are put in I want a list of the owners stored separately like that are. I have my program write it and it works fine but when I try to read from the XML file I get the applications but the separate owners list doesn't read at all. Here is my code that I have:

Expand|Select|Wrap|Line Numbers
  1.  Dim x As Integer
     Dim z As Integer
                '//Declare XML Reader
  2.             Dim Reader As New XmlDocument()
  3.             Reader.Load("ProData.xml")
     '//Try reading Applications
      Try
  4.  
  5.             '//Read Application Element
  6.             Dim nods As XmlNodeList = Reader.SelectNodes("//Application")
  7.             For Each nod As XmlNode In nods
                    '//Redim Arrays
  8.                 ReDim Preserve AppName(2)
  9.                 ReDim Preserve AppKey(2)
  10.                 ReDim Preserve AppOwner(2)
  11.  
  12.                 '//Set AppName/AppKey/AppOwner
  13.                 AppName(x) = nod.Attributes("Name").Value
  14.                 AppKey(x) = nod.Attributes("Key").Value
  15.                 AppOwner(x) = nod.Attributes("Owner").Value
  16.  
  17.                 '//Add to index
  18.                 x += 1
  19.                 appcount += 1
  20.                 cmbList.Items.Add(AppName(x))
                Next nod
  21.  
  22.         Catch ex As Exception
  23.  
  24.         End Try
  25.  
  26.         '//Try reading Owners
  27.         Try
  28.             '//Read Owner Element
  29.             Dim OwnerNods As XmlNodeList = Reader.SelectNodes("/Data/Owner")
  30.             For Each nod As XmlNode In OwnerNods
  31.  
  32.                 ReDim Preserve OwnLst(2)
  33.                 '//Set Owners
  34.                 OwnLst(z) = nod.Attributes("Name").Value
  35.  
  36.                 '//Add to index
  37.                 z += 1
                Next nod
  38.         Catch ex As Exception
            End Try
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 699
#2: Sep 23 '09

re: Reading XML nodes


If you want to read a particular element then you can use : XmlDocument.GetElementsByTagName
Reply

Tags
nodes, read, vb.net, xml


Similar Visual Basic .NET bytes