How to parse XML with VB .net
<p>I have an XML file that I want to convert to a class for use in VB net.</p>

<pre><code>&lt;?xml version="1.0" encoding="windows-1252"?&gt;
&lt;MatML_Doc&gt;
  &lt;Material&gt;
        &lt;BulkDetails&gt;
            &lt;Name&gt;23133385&lt;/Name&gt;
            &lt;Class&gt;
                &lt;Name&gt;1 - Carbon Steel&lt;/Name&gt;
            &lt;/Class&gt;
            &lt;Source source=""/&gt;
            &lt;PropertyData property="Material Type"&gt;
                &lt;Data format="string"&gt;IsotropicMaterial&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Mass Density (RHO)_1"&gt;
                &lt;Data format="exponential"&gt;7.87e-6&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Organization"&gt;
                &lt;Data format="string"&gt;SAE&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Name"&gt;
                &lt;Data format="string"&gt;J1199&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Grade"&gt;
                &lt;Data format="string"&gt;CLASS 9.8&lt;/Data&gt;
            &lt;/PropertyData&gt;
        &lt;/BulkDetails&gt;
    &lt;/Material&gt;
    &lt;Material&gt;
        &lt;BulkDetails&gt;
            &lt;Name&gt;23133419&lt;/Name&gt;
            &lt;Class&gt;
                &lt;Name&gt;1 - Carbon Steel&lt;/Name&gt;
            &lt;/Class&gt;
            &lt;Source source=""/&gt;
            &lt;PropertyData property="Material Type"&gt;
                &lt;Data format="string"&gt;IsotropicMaterial&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Mass Density (RHO)_1"&gt;
                &lt;Data format="exponential"&gt;7.87e-6&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Organization"&gt;
                &lt;Data format="string"&gt;EN&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Name"&gt;
                &lt;Data format="string"&gt;10130&lt;/Data&gt;
            &lt;/PropertyData&gt;
            &lt;PropertyData property="Spec Grade"&gt;
                &lt;Data format="string"&gt;DC05&lt;/Data&gt;
            &lt;/PropertyData&gt;
        &lt;/BulkDetails&gt;
    &lt;/Material&gt;
...
</code></pre>

<p>And I am trying to convert it to a list of a custom class:</p>

<pre><code>Public Class MyMaterial
    Public Name As String
    Public Class As String
    Public Org As String
    Public Spec As String
    Public Grade As String
    Public Density As Double
End Class
</code></pre>

<p>I am lost on how to loop through the xml to create a List(of MyMaterial). Currently I am using stringreader to go through a line at a time running conditional checks in order to create this list. There has to be a more efficient way using the vb.net xml tools.</p>

<p><strong>Solution</strong></p>

<p>Using the help below, this was the solution that worked for me.</p>

<pre><code>Dim doc As XDocument = XDocument.Load(XMLLocation)
            Dim XMLMaterials As IEnumerable(Of XElement) = doc.Root.Elements("Material")
            For Each XEL1 As XElement In XMLMaterials
                Dim material As New MyMaterial
                material.Name = XEL1.Element("BulkDetails").Element("Name").Value
                material.Category = XEL1.Element("BulkDetails").Element("Class").Element("Name").Value
                For Each XEL2 As XElement In XEL1.Element("BulkDetails").Elements.Where(Function(d) d.Name = "PropertyData")
                    If XEL2.Attribute("property").Value = "Mass Density (RHO)_1" Then
                        material.Density = XEL2.Value
                    ElseIf XEL2.Attribute("property").Value = "Spec Organization" Then
                        material.Org = XEL2.Value
                    ElseIf XEL2.Attribute("property").Value = "Spec Name" Then
                        material.Spec = XEL2.Value
                    ElseIf XEL2.Attribute("property").Value = "Spec Grade" Then
                        material.Grade = XEL2.Value
                    End If
                Next
                MaterialsList.Add(material)
                If Not CatagoryNames.Contains(material.Category) Then CatagoryNames.Add(material.Category)
                If Not Organizations.Contains(material.Org) Then Organizations.Add(material.Org)
            Next
</code></pre>