easy XML question
-
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
-
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
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 :-)
-
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 :-)
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 SubPublic Function ReadItem(ByVal inItemName As String) As String
Dim returnSettingValue As StringreturnSettingValue = _dsRegistrationValues.Tables(0).Rows(0).Item(inItemName)
Return returnSettingValue
End FunctionI got the idea from this article, if you are interested http://www.codeproject.com/vb/net/ConfigOpt.asp[^] /jason
-
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 SubPublic Function ReadItem(ByVal inItemName As String) As String
Dim returnSettingValue As StringreturnSettingValue = _dsRegistrationValues.Tables(0).Rows(0).Item(inItemName)
Return returnSettingValue
End FunctionI got the idea from this article, if you are interested http://www.codeproject.com/vb/net/ConfigOpt.asp[^] /jason
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 :-)