I am trying to read xml into a treeview with xmlreader. Xml files that follow <column> </column> notation works fine with my current code, however, if occationally a field is terminated with a slash at the end (ie. <column data /> the treeview becomes skewed and doesn't display properly. How can i have xmlreader recognize this slash as a complete element with an endelement? Below is the code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Clears the Treeview control
TreeView1.Nodes.Clear()
' Creates a new XmlReader and reads the XML file found in the open file
' dialog box.
Dim aReader As XmlReader
OpenFileDialog1.ShowDialog()
If Not OpenFileDialog1.FileName = "" Then
aReader = XmlReader.Create(OpenFileDialog1.FileName)
End If
Dim aNode As TreeNode
Dim bNode As TreeNode
Dim parentNode As TreeNode
' Loops through the nodes of the XML file
While aReader.Read
Select Case aReader.NodeType
' If an element is found, it creates a new node, reads the
' attributes, and Adds the name of the element and the attributes
' as a new node to the Treeview
Case XmlNodeType.Element
aNode = New TreeNode(aReader.Name)
If aReader.AttributeCount > 0 Then
Dim i As Integer
aReader.MoveToFirstAttribute()
aNode.Text &= " " & aReader.Name & "=" & aReader.Value
For i = 1 To aReader.AttributeCount - 1
aReader.MoveToNextAttribute()
aNode.Text &= " " & aReader.Name & "=" & aReader.Value
Next
End If
If parentNode Is Nothing Then
TreeView1.Nodes.Add(aNode)
Else
parentNode.Nodes.Add(aNode)
End If
parentNode = aNode
' When an EndElement is encountered, the parent element is set
' one level up.
Case XmlNodeType.EndElement
parentNode = parentNode.Parent
' When a Text node is encounterd, a new node is made as a child
' node containing the text is created
Case XmlNodeType.Text
bNode = New TreeNode(aReader.Value)
aNode.Nodes.Add(bNode)
Case XmlNodeType.CDATA
bNode = New TreeNode(aReader.Value)
aNode.Nodes.Add(bNode)
Case Else
End Select
End While
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Clears the Treeview control
TreeView1.Nodes.Clear()
' Creates a new XmlReader and reads the XML file found in the open file
' dialog box.
Dim aReader As XmlReader
OpenFileDialog1.ShowDialog()
If Not OpenFileDialog1.FileName = "" Then
aReader = XmlReader.Create(OpenFileDialog1.FileName)
End If
Dim aNode As TreeNode
Dim bNode As TreeNode
Dim parentNode As TreeNode
' Loops through the nodes of the XML file
While aReader.Read
Select Case aReader.NodeType
' If an element is found, it creates a new node, reads the
' attributes, and Adds the name of the element and the attributes
' as a new node to the Treeview
Case XmlNodeType.Element
aNode = New TreeNode(aReader.Name)
If aReader.AttributeCount > 0 Then
Dim i As Integer
aReader.MoveToFirstAttribute()
aNode.Text &= " " & aReader.Name & "=" & aReader.Value
For i = 1 To aReader.AttributeCount - 1
aReader.MoveToNextAttribute()
aNode.Text &= " " & aReader.Name & "=" & aReader.Value
Next
End If
If parentNode Is Nothing Then
TreeView1.Nodes.Add(aNode)
Else
parentNode.Nodes.Add(aNode)
End If
parentNode = aNode
' When an EndElement is encountered, the parent element is set
' one level up.
Case XmlNodeType.EndElement
parentNode = parentNode.Parent
' When a Text node is encounterd, a new node is made as a child
' node containing the text is created
Case XmlNodeType.Text
bNode = New TreeNode(aReader.Value)
aNode.Nodes.Add(bNode)
Case XmlNodeType.CDATA
bNode = New TreeNode(aReader.Value)
aNode.Nodes.Add(bNode)
Case Else
End Select
End While
End Sub