title: VB.NET: Read Multiple Properties and Tables from XML File
body: <p>I have decided to save some application data using XML files.  I am very new to XML files, so bear with me.</p>
  
  <p>I have a class like this):</p>
  
  <pre><code>Public Class config
      Public Property name As String
      Public Property type As String
  
      Public Property inputsTable As DataTable
      Public Property outputsTable As DataTable
  
      Public Sub WriteXML(filePath As String)
          Using writer As XmlWriter = XmlWriter.Create(filePath)
              writer.WriteStartDocument()
              writer.WriteStartElement("config")
  
              writer.WriteElementString("name", Me.name)
              writer.WriteElementString("type", Me.type)
  
              Me.inputsTable.WriteXml(writer)
              Me.outputstable.WriteXml(writer)
  
              writer.WriteEndElement()
              writer.WriteEndDocument()
          End Using
      End Sub
  End Class
  </code></pre>
  
  <p>WriteXML sub results in a file like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;config&gt;
      &lt;name&gt;testConfigName&lt;/name&gt;
      &lt;type&gt;testConfigType&lt;/type&gt;
      &lt;DocumentElement&gt;
          &lt;inputs&gt;
              &lt;inputName&gt;testInputName1&lt;/inputName&gt;
              &lt;inputValue&gt;testInputValue1&lt;/inputValue&gt;
          &lt;/inputs&gt;
          &lt;inputs&gt;
              &lt;inputName&gt;testInputName2&lt;/inputName&gt;
              &lt;inputValue&gt;testInputValue2&lt;/inputValue&gt;
          &lt;/inputs&gt;
      &lt;/DocumentElement&gt;
      &lt;DocumentElement&gt;
          &lt;outputs&gt;
              &lt;outputName&gt;testOutputName1&lt;/outputName&gt;
              &lt;outputValue&gt;testOutputValue1&lt;/outputValue&gt;
          &lt;/outputs&gt;
      &lt;/DocumentElement&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p>I have several questions:</p>
  
  <ol>
  <li><p>What is the purpose of the "root node" in the XML file?  Here I
  created a node "config" in order to get my code working, but I don't
  really understand why it is necessary.</p></li>
  <li><p>Is WriteStartDocument/WriteEndDocument required?</p></li>
  <li><p>How can I write a sub to read this file back into my application?  I am particularly having trouble getting the tables out.  I am able to get the single elements out using:</p>
  
  <pre><code>Using reader As XmlReader = XmlReader.Create(filePath)
      While reader.Read()
          Select Case reader.NodeType
              Case XmlNodeType.Element
                  Select Case reader.Name
                      Case "name"
                          name = reader.ReadElementContentAsString()
                      Case "type"
                          type = reader.ReadElementContentAsString()
                  End Select    
          End Select
      End While
  End Using
  </code></pre>
  
  <p>but I don't know how (or if it is possible) to combine this with:</p>
  
  <pre><code>inputsTable.ReadXml(reader)
  outputsTable.ReadXml(reader)
  </code></pre>
  
  <p>It seems like the ReadXml may not actually work for what I'm trying to do (reading specific tables) and is meant for simpler single-table XML structures, but I could not confirm that definitively.</p></li>
  </ol>
  
  <p>Any help will be greatly appreciated!</p>