title: Read XML file structure and export to strings
body: <p>Im trying to modify the code given in the answer to pull more information from the xml file than was originally put in but im getting some errors</p>
  
  <p>[Code]</p>
  
  <pre><code>Imports System.Xml
  Imports System.Xml.Linq
  Imports System.IO
  Public Class Form1
      Public FILENAME As String = String.Empty
      Public models As Model
      Public productionBlocks As New Panel
      Public pictureBlocks As New List(Of PictureBox)
      Public gridsinfo As CubeGrid
  
      Public dictCount As New Dictionary(Of String, Integer)
  
      Public Sub New()
  
          ' This call is required by the designer.
          InitializeComponent()
          AddHandler Me.Load, AddressOf Form1_Load
          ' Add any initialization after the InitializeComponent() call.
  
      End Sub
  
  
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load
          Dim fileloader As New OpenFileDialog
          fileloader.ShowDialog()
          FILENAME = fileloader.FileName
  
          models = New Model
          models.Load(FILENAME)
  
          Dim blockNames As List(Of String) = models.print.Select(Function(x) x.cubes.Select(Function(y) y.cubeBlocks.Select(Function(z) z.SubtypeName)).SelectMany(Function(y) y).ToList()).FirstOrDefault()
          Dim gridSizeEnumerator As String = models.print.Select(Function(x) gridsinfo.gridSizeEnum).ToString
          dictCount = blockNames.GroupBy(Function(x) x).ToDictionary(Function(x) x.Key, Function(y) y.Count)
          Dim display As String = models.print2.Select(Function(x) x.cubes.Select(Function(y) y.displayname)).SelectMany(Function(y) y).ToString().First()
          Dim enumer As String = models.print3.Select(Function(x) x.cubes.Select(Function(y) y.enumerator)).SelectMany(Function(y) y).ToString().First()
  
  
          For Each item In dictCount
              InfluenceListBox1.AddItem(item)
          Next item
  
  
  
          For Each key As String In dictCount.Keys.AsEnumerable()
              TextBox1.Text = "subTypeName = '{0}', count = '{1}'" + key.ToString + dictCount(key).ToString + Environment.NewLine
          Next key
  
  
  
          If InfluenceListBox1.lstbox.Items.Item(1).ToString.Contains("Large") Then
              InfluenceTextBox1.Text = "Grid Size: Large Ship"
          ElseIf InfluenceListBox1.lstbox.Items.Item(1).ToString.Contains("Small") Then
              InfluenceTextBox1.Text = "Grid Size: Small Ship"
          End If
  
          InfluenceTextBox2.Text = "Grid Owner: " + display.ToString
          InfluenceTextBox3.Text = "Grid Size Enumerator: " + enumer.ToString
      End Sub
  End Class
  Public Class Model
      Public print As New List(Of Model)
      Public print2 As New List(Of Model)
      Public print3 As New List(Of Model)
  
      Public _type As String
      Public _id As ID
      Public _display As String
      Public _display1 As String
      Public displayname As String
      Public enumerator As String
      Public cubes As List(Of CubeGrid)
      Public info As String
  
      Public Sub Load(filename As String)
  
          Dim reader As New StreamReader(filename)
  
          Dim doc As XDocument = XDocument.Load(reader)
          Dim firstNode As XElement = doc.FirstNode
          Dim xsiNs = firstNode.GetNamespaceOfPrefix("xsi")
          Dim drawingTypes = firstNode.Elements.FirstOrDefault()
          Dim drawingType = drawingTypes.Elements.FirstOrDefault()
          Dim drawingStr = drawingType.Name.LocalName
  
          print = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
             ._type = x.Attribute(xsiNs + "type"), ._id = x.Elements("Id").Select(Function(y) New ID() With {
                                                .type = y.Attribute("Type"),
                                                .subtype = y.Attribute("Subtype")
                                            }).FirstOrDefault(),
             .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                                                        .id = y.Element("EntityId"),
                                                        .persistentFlags = y.Element("PersistentFlags"),
                                                        .position = y.Descendants("Position").Select(Function(z) New location() With {
                                                          .x = CType(z.Attribute("x"), Double),
                                                          .y = CType(z.Attribute("y"), Double),
                                                          .z = CType(z.Attribute("z"), Double)
                                                        }).FirstOrDefault(),
                                                        .forward = y.Descendants("Forward").Select(Function(z) New location() With {
                                                          .x = CType(z.Attribute("x"), Double),
                                                          .y = CType(z.Attribute("y"), Double),
                                                          .z = CType(z.Attribute("z"), Double)
                                                        }).FirstOrDefault(),
                                                        .up = y.Descendants("Up").Select(Function(z) New location() With {
                                                          .x = CType(z.Attribute("x"), Double),
                                                          .y = CType(z.Attribute("y"), Double),
                                                          .z = CType(z.Attribute("z"), Double)
                                                        }).FirstOrDefault(),
                                                        .orientation = y.Descendants("Orientation").Select(Function(z) New location() With {
                                                          .w = CType(z.Element("W"), Double),
                                                          .x = CType(z.Element("X"), Double),
                                                          .y = CType(z.Element("Y"), Double),
                                                          .z = CType(z.Element("Z"), Double)
                                                        }).FirstOrDefault(),
                                                        .cubeBlocks = y.Descendants("MyObjectBuilder_CubeBlock").Select(Function(z) New CubeBlock() With {
                                                          .SubtypeName = z.Element("SubtypeName")
                                                        }).ToList()
                                                    }).ToList()
          }).ToList()
  
          print2 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
              .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
                  .displayname = y.Element("DisplayName")
              }).ToList()
          }).ToList()
  
          print3 = doc.Descendants(drawingStr).Select(Function(x) New Model() With {
          .cubes = x.Descendants("CubeGrid").Select(Function(y) New CubeGrid() With {
              .enumerator = y.Element("GridSizeEnum")
              }).ToList()
          }).ToList()
      End Sub
  End Class
  Public Class ID
      Public type As String
      Public subtype As String
  End Class
  Public Class CubeGrid
      Public id As String
      Public persistentFlags As String
      Public position As location
      Public forward As location
      Public up As location
      Public orientation As location
      Public cubeBlocks As List(Of CubeBlock)
  
      Public displayname As String
      Public enumerator As String
  End Class
  Public Class location
      Public w As Double
      Public x As Double
      Public y As Double
      Public z As Double
  End Class
  Public Class CubeBlock
      Public SubtypeName As String
      Public username As String
  End Class
  </code></pre>
  
  <p>[/code]</p>
  
  <p>The error is when converting the label text to the display-name from string throwing an invalid casting exception i would also like to add more items such as the Grid name and grid size enumerator so i can remove the ifcontains() methods on the listbox any help would be greatly appreciated!</p>