Search code examples
vb.netlistviewtextboxxmlreader

XML Reader Variables VB.NET


Working on a management program for basic files and text.

Now, so far the program is saving information from multiple textboxes into an .xml file correctly.

My problem is where I need to load saved files back into Textboxes. Here is another thread I made to Microsoft explaining my issue;

Right, so the code I currently have and use;

Private Sub Objectlist1_ItemActivate(sender As Object, e As EventArgs) Handles Objectlist1.ItemActivate
    Caseworker.Show() 
    Me.Objectlist1.MultiSelect = False

    Dim selectedListViewItem As String
    selectedListViewItem = Me.Objectlist1.SelectedItems.Item(0).ToString

    If (IO.File.Exists(selectedListViewItem + "C:\Users\USER\Desktop\Testfolder-data")) Then
        Dim document As XmlReader = New XmlTextReader(selectedListViewItem + "C:\Users\USER\Desktop\Testfolder-data")

        While (document.Read())
            Dim type = document.NodeType

            If (type = XmlNodeType.Element) Then
                If (document.Name = "Person") Then
                    Caseworker.Pholderbox.Text = document.ReadInnerXml.ToString()
                End If

                If (document.Name = "Driver") Then
                    Caseworker.Driverbox.Text = document.ReadInnerXml.ToString()

Problem here is that I want to be able to click a file in the Listview called "Objectlist1" and the program loads the xml file - Instead of directing the program to One file

As such

If (IO.File.Exists("MyXML.xml")) Then
    Dim document As XmlReader = New XmlTextReader("MyXML.xml)

Apparently there is this variable out there that would be perfect for my issue, but I have looked for it for 2 working days and not been able to track it down.

If there is another stuff I need to add to make this thing work, I appreciate any help you can provide.

Am I far off here guys?


Solution

  • A few things:

    You need the full path to the XMLFile, not just its name. You could do it like this (warning: untested):

    const string basepath= @"C:\Users\USER\Desktop\Testfolder-data"
    
    xmlpath = IO.Path.Combine(basepath, Objectlist1.SelectedItems.Item(0).Text)
    If(IO.File.Exists(xmlpath))
    

    Instead of multiple Ifs, I would recommend

    switch(document.Name)
      {
          Case "Person":
            Caseworker.Pholderbox.Text=...
            break;
    
          Case "Driver":
          ...
      }
    

    If you rename "Pholderbox" to "Personbox", you could spare the entire If/switch and simply do something like:

    var textbox = document.name + "box";
    (TextBox)Caseworker.Controls[textbox].Text=document.InnerText;
    

    Hope this gets you going.