5

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the proper way of pointing this to the actual xml file? I tried loading an XMLDocument type but keep getting errors for members not found.
@Todd - Use the XDocument class to load your file.
1

This answer is more for the question: How to Parse an XML file to create a class in VB.net.

You could auto-generate a class from an XML file to VB by using the xsd.exe tool which comes as part of the Dev tools in Visual Studio.

You will need to open the Dev Command Prompt to run the tool. Open Command Prompt from inside Visual Studio

The default location for Command Prompt for VS 2013 is here, but this could change according to installation location.

In the Command Prompt run the following commands:

xsd 'yourfilename'.xml /classes /language:vb

Example: enter image description here This will auto-generate the XML Scheme File 'yourfilename'.xsd in the directory folder

and next run this:

xsd 'yourfilename'.xsd /classes /language:vb

Example: enter image description here This will auto-generate a VB class from the XSD file which will be contained in a VB file called 'yourfilename'.vb.

Then you can simply add the VB file with your class to your project.

Once you have added this class to your project you can easily loop through the nodes of the class.

1 Comment

This answer deserves many more upvotes. While xsd is not really good, using classes on xml files is a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.