Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. easy XML question

easy XML question

Scheduled Pinned Locked Moved Visual Basic
tutorialcomxmlhelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jcrussell
    wrote on last edited by
    #1

    I have a very simple xml file mail.not-real.com.au from@not-real.com.au Jason jason@not-real.com.au and I want to read a single value out of that, for example: I don't know how to do this This is the code I have at the moment (inItemName = "HostName") Try xmlobjReader = New XmlTextReader(_XMLPath) xmlobjReader.WhitespaceHandling = WhitespaceHandling.None While xmlobjReader.Read If xmlobjReader.Name.ToLower = inItemName.ToLower Then itemValue = xmlobjReader.Value End If End While Catch ex As Exception itemValue = "" Finally If Not xmlobjReader Is Nothing Then xmlobjReader.Close() End If End Try At the end of this code itemValue == "" I know this should be easy, but I can't see what I am doing wrong. Thanks for any help /jason

    S 1 Reply Last reply
    0
    • J jcrussell

      I have a very simple xml file mail.not-real.com.au from@not-real.com.au Jason jason@not-real.com.au and I want to read a single value out of that, for example: I don't know how to do this This is the code I have at the moment (inItemName = "HostName") Try xmlobjReader = New XmlTextReader(_XMLPath) xmlobjReader.WhitespaceHandling = WhitespaceHandling.None While xmlobjReader.Read If xmlobjReader.Name.ToLower = inItemName.ToLower Then itemValue = xmlobjReader.Value End If End While Catch ex As Exception itemValue = "" Finally If Not xmlobjReader Is Nothing Then xmlobjReader.Close() End If End Try At the end of this code itemValue == "" I know this should be easy, but I can't see what I am doing wrong. Thanks for any help /jason

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      Jason, The parsing of the XML was not quite correct. Try this amended code.

      		Try
      			xmlobjReader = New XmlTextReader(\_XMLPath)
      			xmlobjReader.WhitespaceHandling = WhitespaceHandling.None
      			xmlobjReader.MoveToContent()
      
      			Dim isHostName As Boolean
      
      			Do While xmlobjReader.Read
      
      				Select Case xmlobjReader.NodeType
      
      					Case XmlNodeType.Element
      						isHostName = (rdrXML.Name.ToUpper = "HOSTNAME")
      
      					Case (XmlNodeType.Text And isHostName)
      						itemValue = xmlobjReader.Value
      
      				End Select
      
      			Loop
      
      		Catch ex As Exception
      			itemValue = ""
      
      		Finally
      			If Not xmlobjReader Is Nothing Then
      				xmlobjReader.Close()
      			End If
      
      		End Try
      

      Step through the code in the debugger and inspect the various variables to see what's happening. ...Steve "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." (Translation: I'll show you the way, but not write the code for you.) I read that somewhere once :-)

      J 1 Reply Last reply
      0
      • S Steve Pullan

        Jason, The parsing of the XML was not quite correct. Try this amended code.

        		Try
        			xmlobjReader = New XmlTextReader(\_XMLPath)
        			xmlobjReader.WhitespaceHandling = WhitespaceHandling.None
        			xmlobjReader.MoveToContent()
        
        			Dim isHostName As Boolean
        
        			Do While xmlobjReader.Read
        
        				Select Case xmlobjReader.NodeType
        
        					Case XmlNodeType.Element
        						isHostName = (rdrXML.Name.ToUpper = "HOSTNAME")
        
        					Case (XmlNodeType.Text And isHostName)
        						itemValue = xmlobjReader.Value
        
        				End Select
        
        			Loop
        
        		Catch ex As Exception
        			itemValue = ""
        
        		Finally
        			If Not xmlobjReader Is Nothing Then
        				xmlobjReader.Close()
        			End If
        
        		End Try
        

        Step through the code in the debugger and inspect the various variables to see what's happening. ...Steve "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." (Translation: I'll show you the way, but not write the code for you.) I read that somewhere once :-)

        J Offline
        J Offline
        jcrussell
        wrote on last edited by
        #3

        Thanks for your help Steve, but in the end I decided not to go that way. I decided it was too slow to loop throw each element in the XML and check it's type etc instead I have used a dataset

        Public Sub New(ByVal inXMLFilePath As String)
        _XMLPath = inXMLFilePath

        _dsRegistrationValues = New DataSet(_DATASET_TABLE_NAME)
        _dsRegistrationValues.ReadXml(_XMLPath)
        End Sub

        Public Function ReadItem(ByVal inItemName As String) As String
        Dim returnSettingValue As String

        returnSettingValue = _dsRegistrationValues.Tables(0).Rows(0).Item(inItemName)

        Return returnSettingValue
        End Function

        I got the idea from this article, if you are interested http://www.codeproject.com/vb/net/ConfigOpt.asp[^] /jason

        S 1 Reply Last reply
        0
        • J jcrussell

          Thanks for your help Steve, but in the end I decided not to go that way. I decided it was too slow to loop throw each element in the XML and check it's type etc instead I have used a dataset

          Public Sub New(ByVal inXMLFilePath As String)
          _XMLPath = inXMLFilePath

          _dsRegistrationValues = New DataSet(_DATASET_TABLE_NAME)
          _dsRegistrationValues.ReadXml(_XMLPath)
          End Sub

          Public Function ReadItem(ByVal inItemName As String) As String
          Dim returnSettingValue As String

          returnSettingValue = _dsRegistrationValues.Tables(0).Rows(0).Item(inItemName)

          Return returnSettingValue
          End Function

          I got the idea from this article, if you are interested http://www.codeproject.com/vb/net/ConfigOpt.asp[^] /jason

          S Offline
          S Offline
          Steve Pullan
          wrote on last edited by
          #4

          That's a neat solution Jason. Thanks for the link. I'll check it out. ...Steve "Give a man a fish and you've fed him for a day. Teach him how to fish and you've fed him for life." (Translation: I'll show you the way, but not write the code for you.) I read that somewhere once :-)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups