Todd Todd - 6 months ago 41
Vb.net Question

How to parse XML with VB .net

I have an XML file that I want to convert to a class for use in VB net.

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


And I am trying to convert it to a list of a custom class:

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


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.

Solution

Using the help below, this was the solution that worked for me.

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

Answer

You can use linq to xml to read and manipulate xml. In order to go through all elements you should use recursive code.

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

    For Each xel In xmlAll.Elements
        Dim material As New MyMaterial
        material.Name = xel.Element("BulkDetails").Element("Name").Value
        material.Classe = xel.Element("BulkDetails").Element("Classe").Element("Name").Value
        material.Org = xel.Element("BulkDetails").Elements.Where(Function(d) d.Name = "PropertyData" And d.Attribute("property").Value = "Material Type").Value
        'Ect
    Next

End Sub

There are also serialization libraries which serialize Xml to classes.

Comments