Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml field teminated with just a "/" at the end of a line. 1

Status
Not open for further replies.

karlf

IS-IT--Management
Mar 24, 2000
15
US
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
 
The behaviour is that for empty element, its EndElement case will not be encountered. Hence, the revert to "ParentNode" of the context node aNode will not happened. Hence, you should be therefore assign aNode to be the "ParentNode" (awaiting for EndElement case to revert back).

There is a boolean property called IsEmptyElement that you've to make use of, like this.
[tt]
Case XmlNodeType.Element
aNode = New TreeNode(aReader.Name)
[blue]Dim bEmpty As Boolean bEmpty = aNode.IsEmptyElement[/blue]
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
[blue]If Not bEmpty Then
parentNode = aNode
End If[/blue]
' When an EndElement is encountered, the parent element is set
' one level up.
[/tt]
 
tsuji,

Thanks for your reply. Everything was good except the line

Dim bEmpty As Boolean bEmpty = aNode.IsEmptyElement

should be

Dim bEmpty As Boolean bEmpty = areader.IsEmptyElement

Thank you again.
 
>[tt] Dim bEmpty As Boolean bEmpty = [red]areader[/red].IsEmptyElement[/tt]
Absolutely, my bad. Thanks for the note.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top