By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

How to Check if XML Node is Empty/Null/Blank

P: 1
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.
<Monitor> </Monitor>

The call that would generate the exception from the application would be:

Textbox1.Text = GetXMLTag("Monitor")

Public Function GetXMLTag(ByVal Tagname As String) As String

Dim XMLSetting As String = " "

'Load the Settings.XML Document
Dim Doc As New XmlDocument

'Retrieve the XML Nodes
Dim SettingsNodes As XmlNodeList
SettingsNodes = Doc.GetElementsByTagName(Tagname)

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

'Return the Node's Value
Return XMLSetting

Catch ex As Exception
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.
Apr 8 '10 #1
Share this Question
Share on Google+
1 Reply

Aimee Bailey
Expert 100+
P: 197
place "Return XMLSetting" on the line below the "End Try", functions must always return a value, even if its empty!
Apr 13 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.