title: Parse XML file in Visual Basic
body: <p><em>Yes, I am aware of the myriad of posts related to this topic; I've been unable to find my answer, however.</em></p>
  
  <p>I'm making a character editor for an RPG and I need a combo box to load things from a file in the Debug folder. I got the saving down pat; the loading is what's getting me.</p>
  
  <p>My rep isn't high enough to post pictures so I'll try to explain it thoroughly.</p>
  
  <p>There are two combo boxes and three buttons.</p>
  
  <p>The first combo box is where the user enters the name of the characters they want and press the "Add" button to save it for later. Then once the user enters all the names of the characters they press the "Save" button which saves them in an XML file that looks like this, for example:</p>
  
  <pre><code>&lt;?xml version="1.0" standalone="yes"?&gt;
  &lt;DocumentElement&gt;
    &lt;character&gt;
      &lt;name0&gt;Banana&lt;/name0&gt;
    &lt;/character&gt;
    &lt;character&gt;
      &lt;name1&gt;Fruit&lt;/name1&gt;
    &lt;/character&gt;
    &lt;character&gt;
      &lt;name2&gt;Grapes&lt;/name2&gt;
    &lt;/character&gt;
    &lt;character&gt;
      &lt;name3&gt;Oranges&lt;/name3&gt;
    &lt;/character&gt;
    &lt;character&gt;
      &lt;name4&gt;Taco&lt;/name4&gt;
    &lt;/character&gt;
    &lt;character&gt;
      &lt;count&gt;5&lt;/count&gt;
    &lt;/character&gt;
  &lt;/DocumentElement&gt;
  </code></pre>
  
  <p>Notice at the end there is always a "character" named <em>count</em>. This is the number of names that were entered. This may or may not continue to be necessary based on what solution you happen to have. I'm always looking for a more efficient way to do this so I will post the code I used for saving the names as well.</p>
  
  <pre><code>   Private Sub saveFile(ByVal filename As String)
  
          count = NameBox.Items.Count
          REM create xml schema
          Dim table As New DataTable("character")
  
  
          Try
              For x = 0 To count
  
                  If x &lt;= count - 1 Then
                      table.Columns.Add(New DataColumn("name" &amp; x.ToString(),     
          System.Type.GetType("System.String")))
                      REM copy character data into datatable
                      Dim row As DataRow = table.NewRow()
                      row("name" &amp; x.ToString()) = NameBox.Items.Item(x)
                      table.Rows.Add(row)
                  End If
  
                  If x = count Then
                      table.Columns.Add(New DataColumn("count", 
          System.Type.GetType("System.String")))
                      Dim row2 As DataRow = table.NewRow()
                      row2("count") = count.ToString()
                      table.Rows.Add(row2)
                  End If
              Next
  
  
              table.WriteXml(filename)
              table.Dispose()
  
  
          Catch ex As Exception
              MessageBox.Show(ex.Message)
          End Try
    End Sub
  
   Private Sub SaveButton_Click(sender As System.Object, e As   
      System.EventArgs) Handles SaveButton.Click
          Save1.DefaultExt = ".char"
          Save1.Filter = "Character Data Files|*.char"
          Save1.Title = "Save Character File"
          Save1.InitialDirectory = Environment.CurrentDirectory
          Dim result As DialogResult
          result = Save1.ShowDialog(Me)
          If result &lt;&gt; Windows.Forms.DialogResult.OK Then Return
          g_filename = Save1.FileName
          saveFile(g_filename)
  
    End Sub
  </code></pre>
  
  <p>Note:</p>
  
  <p>Save1 is a SaveFileDialog. NameBox is the first combo box. SupportBox is the second. These are my globals— g_filename as String, count as Integer</p>
  
  <p>Now, let's get into the actually loading of this file. Here's what I've tried and of course I get the notorious error "Object reference is not set to the instance of an object" which is the elaborate way of saying "You done goofed with indexing or whatever."</p>
  
  <p>I've tried various things online and from my own knowledge. Instead of posting all of my attempts I'll just leave it at this.</p>
  
  <p>Basically, what I'm wanting is for the file to be loaded into the second combo box (SupportBox) upon pressing the Load button.</p>
  
  <p>The <em>count</em>, as you may guess, was seemingly needed for inclusion in the file so the loop would know how far to go when loading all the things in the file, in the attempt of preventing index errors; it was also needed so that the program would work without initially loading in the names, thus adding to the count. The Load button needs to function alone.</p>
  
  <p>How would I go about loading this file?</p>
  
  <p>Thanks in advance for the help.</p>
  
  <p>Edit:</p>
  
  <p>As per request, I'll post some code on <em>one</em> attempt I made to load this file.</p>
  
  <pre><code>Private Sub loadName(ByVal filename As String)
      Try
  
          Dim doc As New XmlDocument()
          doc.Load(filename)
          Dim list As XmlNodeList = doc.GetElementsByTagName("character")
  
          Dim element As XmlElement = list(0)
  
          For x = 1 To count
              SupportBox.Items.Add(getElement("Name" &amp; x.ToString(), element))
          Next
  
      Catch ex As Exception
          MessageBox.Show(ex.Message)
  
      End Try
  
  End Sub
  
  Private Function getElement(ByVal field As String, ByRef element As   
     XmlElement) As String
      Dim value As String = ""
      Try
          value = element.GetElementsByTagName(field)(0).InnerText
      Catch ex As Exception
          REM ignore error, just return empty
  
          MessageBox.Show(ex.Message)
  
      End Try
      Return value
  End Function
  </code></pre>