Hello, i've been searching quite a while but haven't found the solution i'm after.

I have a project where I need to show clients in a listbox imported from a XML file so far that is what I have but I also need to able to show the related data when I select the name in the listbox

This is my code for getting them in a listbox:

Dim doc As New Xml.XmlDocument

        doc.Load("input.xml")

        Dim xmlTitles As Xml.XmlNodeList = doc.GetElementsByTagName("name")

        For i As Integer = 0 To xmlTitles.Count - 1

            ListBox1.Items.Add(xmlTitles(i).FirstChild.Value)

        Next

and here is the XML file I have to work with:

<?xml version="1.0"?>

-<clients>


-<client id="1">

<name>Acrobedding (Cloud)</name>

<sender>cloud@acrobedding.com</sender>

<errorWords>failed|error</errorWords>

<falseWords>Warnings: []|Errors: []|Failed to process path:|FilesWithError: 0|VerboseErrors:</falseWords>

<link>jent@intlight.be</link>

<days>MA|DI|WO|DO|VR</days>

</client>


-<client id="2">

<name>ABF (Cloud)</name>

<sender>cloud@abf.be</sender>

<errorWords>failed|error</errorWords>

<falseWords>Warnings: []|Errors: []|Failed to process path:|FilesWithError: 0|VerboseErrors:</falseWords>

<link>jent@intlight.be</link>

<days>MA|DI|WO|DO|VR</days>

</client>

So for example I have to be able to select the customer (Acrobedding (Cloud)) and then the Errorwords and falsewords related to that name have to show up in a textbox or something like that.

If anyone has a solution for my problem it would be much appreciated.

Thanks in advance

Recommended Answers

All 8 Replies

There may be a simpler solution, but this one will work:

Client.vb

Public Class Client
    Private _id As String = String.Empty
    Private _name As String = String.Empty
    Private _sender As String = String.Empty
    Private _errorWords As String = String.Empty
    Private _falseWords As String = String.Empty
    Private _link As String = String.Empty
    Private _days As String = String.Empty

    Public Sub New()

    End Sub

    Public Sub New(ByVal id As String, ByVal name As String, _
                   ByVal sender As String, ByVal errorWords As String, _
                   ByVal falseWords As String, ByVal link As String, ByVal days As String)

        _id = id
        _name = name
        _sender = sender
        _errorWords = errorWords
        _falseWords = falseWords
        _link = link
        _days = days
    End Sub


    Public Property Id As String
        Get
            Return _id
        End Get

        Set(value As String)
            _id = value
        End Set
    End Property 'Id

    Public Property Name As String
        Get
            Return _name
        End Get

        Set(value As String)
            _name = value
        End Set
    End Property 'Name

    Public Property Sender As String
        Get
            Return _sender
        End Get

        Set(value As String)
            _sender = value
        End Set
    End Property 'Sender

    Public Property ErrorWords As String
        Get
            Return _errorWords
        End Get

        Set(value As String)
            _errorWords = value
        End Set
    End Property 'ErrorWords

    Public Property FalseWords As String
        Get
            Return _falseWords
        End Get

        Set(value As String)
            _falseWords = value
        End Set
    End Property 'FalseWords

    Public Property Link As String
        Get
            Return _link
        End Get

        Set(value As String)
            _link = value
        End Set
    End Property 'Link

    Public Property Days As String
        Get
            Return _days
        End Get

        Set(value As String)
            _days = value
        End Set
    End Property 'Days


End Class

Need to add Imports System.Xml to Form1.vb

Form1.vb

    Private Function readXmlFile(ByVal filename As String) As List(Of Client)
        Dim sb As String = String.Empty
        Dim myClient As Client = New Client()
        Dim textReader As XmlTextReader
        Dim currentInfo As String = String.Empty
        Dim myElementName As String = String.Empty
        Dim myAttributeName As String = String.Empty
        Dim myClientList As List(Of Client)

        myClientList = New List(Of Client)
        textReader = New XmlTextReader(filename)

        textReader.MoveToContent()

        While (textReader.Read())
            currentInfo = String.Empty

            Select Case (textReader.NodeType)
                Case XmlNodeType.Element

                    currentInfo = "Element name: " + textReader.Name
                    myElementName = textReader.Name
                    sb += currentInfo
                    'Console.WriteLine(currentInfo)

                    myAttributeName = String.Empty
                    While (textReader.MoveToNextAttribute())
                        currentInfo = "Attribute: " + textReader.Name + "=" + textReader.Value
                        myAttributeName = textReader.Name
                        sb += currentInfo
                        'Console.WriteLine(currentInfo)

                        If (String.Compare(textReader.Name, "name") = 0 And String.Compare(myAttributeName, "id") = 0) Then
                            'intialize myClient for each new client
                            myClient = New Client()

                            myClient.Id = textReader.Value
                        End If
                    End While

                Case XmlNodeType.Text
                    currentInfo = "Text: " + textReader.Value
                    sb += currentInfo
                    'Console.WriteLine(currentInfo)

                    'Console.WriteLine("myElementName: " + myElementName + " val: " + textReader.Value)
                    If (String.Compare(textReader.Value, String.Empty) <> 0) Then

                        If (String.Compare(myElementName, "name") = 0) Then
                            myClient.Name = textReader.Value

                        ElseIf (String.Compare(myElementName, "sender") = 0) Then
                            myClient.Sender = textReader.Value

                        ElseIf (String.Compare(myElementName, "errorWords") = 0) Then
                            myClient.ErrorWords = textReader.Value

                        ElseIf (String.Compare(myElementName, "falseWords") = 0) Then
                            myClient.FalseWords = textReader.Value

                        ElseIf (String.Compare(myElementName, "link") = 0) Then
                            myClient.Link = textReader.Value

                        ElseIf (String.Compare(myElementName, "days") = 0) Then
                            myClient.Days = textReader.Value

                            'add client to arraylist
                            myClientList.Add(New Client(myClient.Id, myClient.Name, myClient.Sender, _
                                                        myClient.ErrorWords, myClient.FalseWords, _
                                                        myClient.Link, myClient.Days))

                        End If
                    End If

                Case XmlNodeType.EndElement
                    currentInfo = "End Element"
                    sb += currentInfo
                    'Console.WriteLine(currentInfo)

                    sb += System.Environment.NewLine
            End Select
        End While

        Return myClientList

    End Function

Usage:

        Dim myList As List(Of Client) = readXmlFile("C:\temp\client.xml")

        For Each myC As Client In myList

            Console.WriteLine(myC.Id + " Name: " + myC.Name + "Link: " + myC.Link)
        Next

I've attached the files. Once the data is in a list, you can just search the list. Or you could probably put the data into a dictionary. Then in the combobox, use the "SelectedValueChanged" or "SelectedIndexChanged" event.

Ok. Here is a better way of doing it.

        Dim xmlName As Xml.XmlNode
        Dim xmlSender As Xml.XmlNode
        Dim xmlErrorWord As Xml.XmlNode
        Dim xmlFalseWord As Xml.XmlNode
        Dim xmlLink As Xml.XmlNode
        Dim xmlDay As Xml.XmlNode
        Dim xmlClientNodeList As Xml.XmlNodeList

        xmlClientNodeList = doc.SelectNodes("//clients/client")

        For Each myNode As Xml.XmlNode In xmlClientNodeList
            If Not (myNode.Attributes Is Nothing) Then
                For Each myAttrib As Xml.XmlAttribute In myNode.Attributes
                    If (String.Compare(myAttrib.Name, "id") = 0) Then
                        Console.WriteLine(myAttrib.Name + ":" + myAttrib.Value)
                    End If
                Next
            End If

            xmlName = doc.SelectSingleNode("/clients/client/name")
            If Not (xmlName Is Nothing) Then
                Console.WriteLine(xmlName.Name + ": " + xmlName.InnerText)
            End If

            xmlSender = doc.SelectSingleNode("/clients/client/sender")
            If Not (xmlSender Is Nothing) Then
                Console.WriteLine(xmlSender.Name + ": " + xmlSender.InnerText)
            End If


            'add the rest here

        Next

Then add them to a List (of Client) below where it says 'add the rest here' (or a Dictionary).

Dear cgeier thank you for all the code you have given but I have no idea where to put the listbox/combobox in the first piece of code you have given. the second piece of code gives me alot of errors when I try it in my vb.net

Thanks in advance
DGianni

When I pasted it, I may have missed a couple of lines. My apologies.

Need to add Imports System.Xml

Uses the "Client.vb" class from above.

    Private Function readXmlFile(ByVal filename As String) As List(Of Client)
        Dim doc As New Xml.XmlDocument
        Dim myClientList As New List(Of Client)
        Dim myClient As Client

        doc.Load("C:\temp\client.xml")

        Dim xmlName As Xml.XmlNode
        Dim xmlSender As Xml.XmlNode
        Dim xmlErrorWord As Xml.XmlNode
        Dim xmlFalseWord As Xml.XmlNode
        Dim xmlLink As Xml.XmlNode
        Dim xmlDay As Xml.XmlNode
        Dim xmlClientNodeList As Xml.XmlNodeList

        xmlClientNodeList = doc.SelectNodes("//clients/client")

        For Each myNode As Xml.XmlNode In xmlClientNodeList
            If Not (myNode.Attributes Is Nothing) Then
                For Each myAttrib As Xml.XmlAttribute In myNode.Attributes
                    If (String.Compare(myAttrib.Name, "id") = 0) Then

                        'intialize new client
                        myClient = New Client()
                        Console.WriteLine(myAttrib.Name + ":" + myAttrib.Value)
                    End If
                Next
            End If

            xmlName = doc.SelectSingleNode("/clients/client/name")
            If Not (xmlName Is Nothing) Then
                myClient.Name = xmlName.InnerText
                Console.WriteLine(xmlName.Name + ": " + xmlName.InnerText)

            End If

            xmlSender = doc.SelectSingleNode("/clients/client/sender")
            If Not (xmlSender Is Nothing) Then
                myClient.Sender = xmlSender.InnerText
                Console.WriteLine(xmlSender.Name + ": " + xmlSender.InnerText)
            End If

            xmlErrorWord = doc.SelectSingleNode("/clients/client/errorWords")
            If Not (xmlSender Is Nothing) Then
                myClient.ErrorWords = xmlErrorWord.InnerText
                Console.WriteLine(xmlErrorWord.Name + ": " + xmlErrorWord.InnerText)
            End If

            'add the rest here

            'add client to arraylist
            myClientList.Add(New Client(myClient.Id, myClient.Name, myClient.Sender, _
                                        myClient.ErrorWords, myClient.FalseWords, _
                                        myClient.Link, myClient.Days))
        Next

        Return (myClientList)
    End Function

Usage:

    Dim myList As List(Of Client) = readXmlFile("C:\temp\client.xml")

    'where ComboBox1 contains "name"
    'where ComboBox2 contains "errorWords"

    ComboBox2.Items.Clear()
    For Each yourClient As Client In myList

        'if this is the one the user selected
        'add data to the second combobox
        If (String.Compare(yourClient.Name, ComboBox1.SelectedText) = 0) Then
            ComboBox2.Items.Add(yourClient.ErrorWords)
        End If

        Console.WriteLine(yourClient.Id + " Name: " + yourClient.Name + "Link: " + yourClient.Link)
    Next

Before, I didn't have time to properly test the code. This one has been tested and hopefully will work for you. I've added some error checking to it.

Use "Client.vb" from my previous post.

To use it you will need a form called "Form1.vb".

Add Imports System.Xml

On "Form1" you need to add the following:

  • add a combobox named: ComboBox1
  • add event "SelectedValueChanged or "SelectedIndexChanged" to ComboBox1 (the code below contains both, but you only need to use one of them)
  • add a textbox named: errorWordsTextBox
  • add a textbox named: falseWordsTextBox

Double-click the form to create "Form1_Load".

Note: The node names are case-sensitive. "Clients" (upper-case first letter) is different from "clients" (all lower-case).

Form1.vb

Imports System.Xml

'To use this:
'add a combobox named: ComboBox1
'add a textbox named: errorWordsTextBox
'add a textbox named: falseWordsTextBox

Public Class Form1
    Dim myList As List(Of Client) = New List(Of Client)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'change this to your filename
        myList = readXmlFile("C:\temp\client.xml")

        'where ComboBox1 contains "name"
        'where errorWordsTextBox contains "errorWords"
        'where falseWordsTextBox contains "falseWords"

        ComboBox1.Items.Clear()
        For Each yourClient As Client In myList
            ComboBox1.Items.Add(yourClient.Name)
            'Console.WriteLine("Id: " + yourClient.Id)
        Next
    End Sub

    Private Function readXmlFile(ByVal filename As String) As List(Of Client)
        Dim doc As New Xml.XmlDocument
        Dim myClientList As New List(Of Client)
        Dim myClient As Client = New Client()

        If Not (System.IO.File.Exists(filename)) Then
            MessageBox.Show(filename + " not found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Return myClientList 'exit function
        End If

        Try
            'load XML document
            doc.Load(filename)

            Dim xmlName As Xml.XmlNode
            Dim xmlSender As Xml.XmlNode
            Dim xmlErrorWord As Xml.XmlNode
            Dim xmlFalseWord As Xml.XmlNode
            Dim xmlLink As Xml.XmlNode
            Dim xmlDay As Xml.XmlNode
            Dim xmlClientNodeList As Xml.XmlNodeList

            'the string passed to SelectNodes is case-sensitive
            xmlClientNodeList = doc.SelectNodes("/clients/client")


            'if node specified in xmlClientNodeList doesn't exist
            'get list of nodes that exist in filename
            'print them, show a messagebox, and return
            If (xmlClientNodeList.Count = 0) Then

                Dim myXml As New Xml.XmlTextReader(filename)
                Dim nodeInfo As String = String.Empty

                nodeInfo = "In '" + filename + "'" + System.Environment.NewLine
                nodeInfo += System.Environment.NewLine
                nodeInfo += "The node you specified in 'doc.SelectNodes()' does not exist." + System.Environment.NewLine
                nodeInfo += "Below is the structure of your XML file." + System.Environment.NewLine
                nodeInfo += System.Environment.NewLine
                nodeInfo += "Note: Node names are case-sensitve." + System.Environment.NewLine
                nodeInfo += System.Environment.NewLine

                Dim previousDepth As Integer = 0
                Dim currentDepth As Integer = 0

                'read xml file
                While myXml.Read
                    If myXml.NodeType = Xml.XmlNodeType.Element Then
                        currentDepth = myXml.Depth

                        'don't print out same node info more than once
                        If (currentDepth >= previousDepth And previousDepth >= 0) Then
                            nodeInfo += Space(4 * myXml.Depth)
                            nodeInfo += "-" + myXml.Name + System.Environment.NewLine
                            previousDepth = currentDepth
                        ElseIf (currentDepth < previousDepth) Then
                            previousDepth = -1
                        End If
                    End If
                End While

                Console.WriteLine("")

                'show node info that exists in XML file
                Console.WriteLine(nodeInfo)
                MessageBox.Show(nodeInfo, "XML File Format", MessageBoxButtons.OK, MessageBoxIcon.Information)

                'close XmlTextReader
                myXml.Close()
            Else
                'get xml data
                For Each xmlClientNode As Xml.XmlNode In xmlClientNodeList
                    If Not (xmlClientNode.Attributes Is Nothing) Then
                        For Each myAttrib As Xml.XmlAttribute In xmlClientNode.Attributes
                            If (String.Compare(myAttrib.Name, "id") = 0) Then

                                'intialize new client
                                myClient = New Client()

                                myClient.Id = myAttrib.Value
                                'Console.WriteLine(myAttrib.Name + ":" + myAttrib.Value)
                            End If
                        Next
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlName = xmlClientNode.SelectSingleNode("name")
                    If Not (xmlName Is Nothing) Then
                        myClient.Name = xmlName.InnerText
                        'Console.WriteLine(xmlName.Name + ": " + xmlName.InnerText)
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlSender = xmlClientNode.SelectSingleNode("sender")
                    If Not (xmlSender Is Nothing) Then
                        myClient.Sender = xmlSender.InnerText
                        'Console.WriteLine(xmlSender.Name + ": " + xmlSender.InnerText)
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlErrorWord = xmlClientNode.SelectSingleNode("errorWords")
                    If Not (xmlErrorWord Is Nothing) Then
                        myClient.ErrorWords = xmlErrorWord.InnerText
                        'Console.WriteLine(xmlErrorWord.Name + ": " + xmlErrorWord.InnerText)
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlFalseWord = xmlClientNode.SelectSingleNode("falseWords")
                    If Not (xmlFalseWord Is Nothing) Then
                        myClient.FalseWords = xmlFalseWord.InnerText
                        'Console.WriteLine(xmlFalseWord.Name + ": " + xmlFalseWord.InnerText)
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlLink = xmlClientNode.SelectSingleNode("link")
                    If Not (xmlLink Is Nothing) Then
                        myClient.Link = xmlLink.InnerText
                        'Console.WriteLine(xmlLink.Name + ": " + xmlLink.InnerText)
                    End If

                    'the string passed to SelectSingleNode is case-sensitive
                    xmlDay = xmlClientNode.SelectSingleNode("days")
                    If Not (xmlDay Is Nothing) Then
                        myClient.Days = xmlDay.InnerText
                        'Console.WriteLine(xmlDay.Name + ": " + xmlDay.InnerText)
                    End If

                    'add client to arraylist
                    myClientList.Add(New Client(myClient.Id, myClient.Name, myClient.Sender, _
                                                myClient.ErrorWords, myClient.FalseWords, _
                                                myClient.Link, myClient.Days))
                Next
            End If

        Catch ex As Exception
            Console.WriteLine("Error: " + ex.Message)
        End Try

        Return (myClientList)
    End Function

    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        'where ComboBox1 contains "name"
        'where errorWordsTextBox contains "errorWords"
        'where falseWordsTextBox contains "falseWords"

        Console.WriteLine("in SelectedIndexChanged")

        'Console.WriteLine("SelectedItem: " + ComboBox1.SelectedItem)
        For Each yourClient As Client In myList

            'if this is the one the user selected
            'add data to the textboxes
            If (String.Compare(yourClient.Name, ComboBox1.SelectedItem) = 0) Then
                errorWordsTextBox.Text = yourClient.ErrorWords
                errorWordsTextBox.Refresh()

                falseWordsTextBox.Text = yourClient.FalseWords
                falseWordsTextBox.Refresh()
            End If

            'Console.WriteLine("Id: " + yourClient.Id)
            'Console.WriteLine("Name: " + yourClient.Name)
            'Console.WriteLine("Sender: " + yourClient.Sender)
            'Console.WriteLine("errorWords: " + yourClient.ErrorWords)
            'Console.WriteLine("falseWords: " + yourClient.FalseWords)
            'Console.WriteLine("Link: " + yourClient.Link)
            'Console.WriteLine("Days: " + yourClient.Days)
            'Console.WriteLine("")
        Next
    End Sub

    Private Sub ComboBox1_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
        'where ComboBox1 contains "name"
        'where errorWordsTextBox contains "errorWords"
        'where falseWordsTextBox contains "falseWords"

        Console.WriteLine("in SelectedValueChanged")

        For Each yourClient As Client In myList

            'if this is the one the user selected
            'add data to the textboxes

            'Console.WriteLine("SelectedItem: " + ComboBox1.SelectedItem)
            If (String.Compare(yourClient.Name, ComboBox1.SelectedItem) = 0) Then
                errorWordsTextBox.Text = yourClient.ErrorWords
                errorWordsTextBox.Refresh()

                falseWordsTextBox.Text = yourClient.FalseWords
                falseWordsTextBox.Refresh()
            End If

            'Console.WriteLine("Id: " + yourClient.Id)
            'Console.WriteLine("Name: " + yourClient.Name)
            'Console.WriteLine("Sender: " + yourClient.Sender)
            'Console.WriteLine("errorWords: " + yourClient.ErrorWords)
            'Console.WriteLine("falseWords: " + yourClient.FalseWords)
            'Console.WriteLine("Link: " + yourClient.Link)
            'Console.WriteLine("Days: " + yourClient.Days)
            'Console.WriteLine("")
        Next
    End Sub
End Class

I've attached the code files.

Hello again cgeier I've copied your last piece of code and the only error i'm getting now are the Clients

When I click on the error it says: Type 'Client' is not defined.

Also the links of code you put in a zipfile aren't obtainable anymore

Thanks in Advance

DGianni

The code for Client.vb is above, in my very first post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.