I know I'm overlooking something very simple but How can I check if the text within an XML tag is blank?
The code below works fine as long as there are values within an XML node (For example the <CPU>Intel</CPU> node below) but it throws an Exception error when the node value is blank/null whatever as in the example node <Monitor> </Monitor>.
It's ok if the node value is blank, I just want to check for it and not throw an exception error.
This question uses the following XML snippet below: Note: The tag <Monitor></Monitor> has no value in it.
<PCSettings>
<SystemSettings >
<CPU>Intel</CPU>
<Monitor> </Monitor>
</SystemSettings>
</PCSettings>
The call that would generate the exception from the application would be:
Textbox1.Text = GetXMLTag("Moni tor")
Public Function GetXMLTag(ByVal Tagname As String) As String
Dim XMLSetting As String = " "
Try
'Load the Settings.XML Document
Dim Doc As New XmlDocument
Doc.Load("Setti ngs.XML")
'Retrieve the XML Nodes
Dim SettingsNodes As XmlNodeList
SettingsNodes = Doc.GetElements ByTagName(Tagna me)
Dim Node As XmlNode
'Cycles through the nodes to find the correct one
For Each Node In SettingsNodes
'If (Node is not Null/Blank) code then continue should probably go here
'This is where I'm stuck
'
XMLSetting = Node.ChildNodes (0).Value 'This works fine if data exists
Next
'Return the Node's Value
Return XMLSetting
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Function
Oh, this also produces the Warning message:
Function 'GetXMLTag' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
And, this is precisely what I'm trying to account for.
Thanks for your response in advance.
The code below works fine as long as there are values within an XML node (For example the <CPU>Intel</CPU> node below) but it throws an Exception error when the node value is blank/null whatever as in the example node <Monitor> </Monitor>.
It's ok if the node value is blank, I just want to check for it and not throw an exception error.
This question uses the following XML snippet below: Note: The tag <Monitor></Monitor> has no value in it.
<PCSettings>
<SystemSettings >
<CPU>Intel</CPU>
<Monitor> </Monitor>
</SystemSettings>
</PCSettings>
The call that would generate the exception from the application would be:
Textbox1.Text = GetXMLTag("Moni tor")
Public Function GetXMLTag(ByVal Tagname As String) As String
Dim XMLSetting As String = " "
Try
'Load the Settings.XML Document
Dim Doc As New XmlDocument
Doc.Load("Setti ngs.XML")
'Retrieve the XML Nodes
Dim SettingsNodes As XmlNodeList
SettingsNodes = Doc.GetElements ByTagName(Tagna me)
Dim Node As XmlNode
'Cycles through the nodes to find the correct one
For Each Node In SettingsNodes
'If (Node is not Null/Blank) code then continue should probably go here
'This is where I'm stuck
'
XMLSetting = Node.ChildNodes (0).Value 'This works fine if data exists
Next
'Return the Node's Value
Return XMLSetting
Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Function
Oh, this also produces the Warning message:
Function 'GetXMLTag' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
And, this is precisely what I'm trying to account for.
Thanks for your response in advance.
Comment