473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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("Monitor")


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("Settings.XML")

'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
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.
Apr 8 '10 #1
1 12253
Aimee Bailey
197 Expert 100+
@jrlittle86
place "Return XMLSetting" on the line below the "End Try", functions must always return a value, even if its empty!
Apr 13 '10 #2

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

Similar topics

3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
2
by: Mark | last post by:
Hi, If I have a DataView containing some items and what I want to do is restore a textbox with an item in one of the rows. If the item in the datarow is null then obviously I get an error when I...
2
by: Paul Telco | last post by:
Hello, I'm a new user designing a simple database to retrieve pre-prepared docunents for printing. I have five tables, a form to design the documents, a form to customise and retrieve the...
11
by: Dennis Allison | last post by:
Which C libraries (current and historical) interpret a null pointer as a pointer to a null (that is, empty) string?
2
by: Xendor | last post by:
Hi, i am a noobie in asp.net and i have the following problem. I use an Form with a textbox to enter a mail-adress. In this textbox a valid email-adress should be entered. But when i leave the...
3
by: dhutton | last post by:
SQL Server 2000 How do you write your query to check for blank (empty) cells? Need to check 3 columns. ColumnA, ColumnB and ColumnC. I need to grab the data in ColumnA ONLY if ColumnB and...
0
by: amollokhande1 | last post by:
Hi All, I am using following query to get the data in xml format strQuery="Select PatternCode,PatternName, Patterntype from TStore" GetXml() method of Dataset returns me following Node...
3
by: akshalika | last post by:
I want regular expression which check for empty. pls help me.
10
by: klharding | last post by:
I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.