How to read null value from XML Node?

rudba

Member
Joined
Jan 15, 2009
Messages
5
Programming Experience
Beginner
Hello,
How to get the null value from the xml node.

Pls. modify the following code to get null value.

VB.NET:
Imports System.Xml
Module Module1

    Sub Main()
        Dim reader As XmlTextReader = New XmlTextReader("bb.xml")

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element 'Display beginning of element.
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            'Display attribute name and value.
                            Console.Write(" {0}='{1}'", reader.Name, reader.Value)
                        End While
                    End If
                Case XmlNodeType.Text 'Display the text in each element.
                    Console.WriteLine(reader.Value)
            End Select
        Loop
        Console.ReadLine()

    End Sub

End Module
 
Last edited by a moderator:
VB.NET:
        If YourField IsNot System.DBNull.Value Then
            'Has a value
        Else
            'Contains a null value
        End If
 
You can also use this way. but @Tom idea is also good..

VB.NET:
Imports System.Xml
Module Module1

    Sub Main()
        Dim reader As XmlTextReader = New XmlTextReader("bb.xml")

        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element 'Display beginning of element.
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            'Display attribute name and value.
                            Console.Write(" {0}='{1}'", cstr(reader.Name), cstr(reader.Value))
                        End While
                    End If
                Case XmlNodeType.Text 'Display the text in each element.
                    Console.WriteLine(cstr(reader.Value))
            End Select
        Loop
        Console.ReadLine()

    End Sub

End Module


with this 'cstr' every null or nothing value will be converted to a string.. like ""
 
Back
Top