Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLoad a ListView control from an XML file in Visual Basic .NET
DescriptionThis example shows how to load a ListView control from an XML file in Visual Basic .NET.
KeywordsListView, load, XML, VB.NET
CategoriesControls, Internet, Files and Directories, Software Engineering
 
When the form loads, the program builds an XmlDocument object holding the XML data. It uses function GetAttributesAndNames to get a list of the attributes and element names in the first Employee node's subtree. It splits the list of names apart and uses them to set the ListView's column headers.

Next the program loops through the file's Employee elements. It calls function GetAttributeAndNameValues to get the attribute and name values corresponding to the column names and adds them to the ListView.

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Compose the XML file's name.
    Dim file_name As String
    file_name = Application.StartupPath
    If file_name.EndsWith("\bin") Then
        file_name = file_name.Substring(0, _
            file_name.LastIndexOf("\bin"))
    End If
    file_name &= "\Employees.xml"

    ' Load the XML file.
    Dim xml_doc As New XmlDocument
    xml_doc.Load(file_name)
    Dim root_node As XmlElement = xml_doc.DocumentElement

    ' Use the first Employee node to make the column
    ' headers.
    Dim employee_node As XmlNode = root_node.FirstChild
    Dim txt As String = GetAttributesAndNames(employee_node)
    Dim values() As String = txt.Split("|"c)
    For Each str As String In values
        Dim col_header As New ColumnHeader
        col_header.Text = str
        col_header.Width = 100
        lvwEmployees.Columns.Add(col_header)
    Next str

    ' Read the Employee values.
    For Each employee_node In root_node.ChildNodes
        txt = GetAttributeAndNameValues(employee_node)
        values = txt.Split("|"c)
        If values.Length > 0 Then
            Dim lvw_item As ListViewItem = _
                lvwEmployees.Items.Add(values(0))
            For i As Integer = 1 To values.Length - 1
                lvw_item.SubItems.Add(values(i))
            Next i
        End If
    Next employee_node

    ' Size the columns to fit.
    For Each hdr As ColumnHeader In lvwEmployees.Columns
        hdr.Width = -2
    Next hdr
End Sub
 
Function GetAttributesAndNames returns the attributes and element names for a node's subtree. It first loops through the node's attributes adding their names to a string. It then loops through the node's child nodes.

If the node contains a single text node, then the child is a leaf-level element so the program adds its name to the result.

If the does not contain a single text node, then the program loops through its children calling function GetAttributesAndNames recursively and adding the results to this call's result.

 
' Return a string giving the element names and attributes
' for this node's subtree separated by | characters.
Private Function GetAttributesAndNames(ByVal xml_node As _
    XmlNode) As String
    Dim result As String = ""

    ' Get this element's attributes.
    For Each attr As XmlAttribute In xml_node.Attributes
        result &= "|" & xml_node.Name & " " & attr.Name
    Next attr

    ' If the element has a single text child,
    ' add the element's name.
    If (xml_node.ChildNodes.Count = 1) And _
       (xml_node.FirstChild.NodeType = XmlNodeType.Text) _
    Then
        ' It's a text node. Use its name.
        result = result & "|" & xml_node.Name
    Else
        ' Get the sub-tree's names and attributes.
        For Each child As XmlNode In xml_node.ChildNodes
            If child.NodeType = XmlNodeType.Element Then
                result = result & "|" & _
                    GetAttributesAndNames(child)
            End If
        Next child
    End If

    If Len(result) > 0 Then result = result.Substring(1)
    Return result
End Function
 
Function GetAttributeAndNameValues works much as function GetAttributesAndNames does except it returns values for attributes and text elements rather than their names.
 
' Return a string giving the values of element names and
' attributes
' for this node's subtree separated by | characters.
Private Function GetAttributeAndNameValues(ByVal xml_node _
    As XmlNode) As String
    Dim result As String = ""

    ' Get this element's attribute values.
    For Each attr As XmlAttribute In xml_node.Attributes
        result &= "|" & attr.Value
    Next attr

    ' If the element has a single text child,
    ' add the element's name.
    If (xml_node.ChildNodes.Count = 1) And _
       (xml_node.FirstChild.NodeType = XmlNodeType.Text) _
    Then
        ' It's a text node. Use its value.
        result = result & "|" & _
            xml_node.FirstChild.InnerText
    Else
        ' Get the sub-tree's names and attributes.
        For Each child As XmlNode In xml_node.ChildNodes
            If child.NodeType = XmlNodeType.Element Then
                result &= "|" & _
                    GetAttributeAndNameValues(child)
            End If
        Next child
    End If

    If Len(result) > 0 Then result = result.Substring(1)
    Return result
End Function
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated