Search code examples
xmlvb.netxml-parsingreadxml

vb.net Reading XMLFile


I have a huge XML File (20'000 Rows) and i want to read some Data out of it. I have written an Routine by now. My Question is, is there a way to make it look better? Because i think that's an ugly way to get the Data out of the File.

This is the Structure of my XML File

<List
<Header>
</>
<Root>
    <Items>
        <Object>
            <Items>
                <Object>
                    <Items>
                        <Object>
                            <Items>
                                <Object>
                                    <IoItems>
                                        <IoObject>
                                        </>
                                    </>
                                </>
                            </>
                            <IoItems>                           
                                <IoObject>
                                </>
                            </>
                        </>
                        <Object>
                        </>
                        <Object>
                        </>
                        <Object>
                        </>
                    </> 
                </> 
                <Object>
                    //Like the Object above
                </>
                <StructureObject>
                    //Like the Object above
                </>
            </> 
        </> 
    </> 
</> 

And my Code looks like that:

Dim myXMLDoc As New XmlDocument
        Dim Node As XmlNode
        Dim Node2 As XmlNode
        Dim Node3 As XmlNode
        Dim Node4 As XmlNode
        Dim Node5 As XmlNode
        Dim Node6 As XmlNode
        Dim Node7 As XmlNode
        Dim Node8 As XmlNode
        Dim Node9 As XmlNode
        Dim Node10 As XmlNode
        Dim Node11 As XmlNode

        myXMLDoc.Load("H:\Data.xml")
        Dim root As XmlNode = myXMLDoc.GetElementsByTagName("Root")(0)

        For Each Node In root.ChildNodes
            If Node.Name = "Items" Then
                For Each Node2 In Node.ChildNodes
                    If Node2.Name = "Object" Then
                        For Each Node3 In Node2.ChildNodes
                            If Node3.Name = "Items" Then
                                For Each Node4 In Node3.ChildNodes
                                    If Node4.Name = "Object" Then
                                        For Each Node5 In Node4.ChildNodes
                                            If Node5.Name = "Items" Then
                                                For Each Node6 In Node5.ChildNodes
                                                    If Node6.Name = "Object" Then
                                                        For Each Node7 In Node6.ChildNodes
                                                            If Node7.Name = "Items" Then
                                                                For Each Node8 In Node6.ChildNodes
                                                                    If Node8.Name = "Object" Then
                                                                        For Each Node9 In Node8.ChildNodes
                                                                            If Node9.Name = "IoItems" Then
                                                                                For Each Node10 In Node9.ChildNodes
                                                                                    If Node10.Name = "IoObject" Then
                                                                                        MsgBox(Node10.Value)
                                                                                    End If
                                                                                Next
                                                                            End If
                                                                        Next
                                                                    End If
                                                                Next
                                                            ElseIf Node6.Name = "IoItems" Then
                                                                For Each Node11 In Node6.ChildNodes
                                                                    If Node11.Name = "IoObject" Then
                                                                        MsgBox(Node11.Value)
                                                                    End If
                                                                Next
                                                            End If
                                                        Next
                                                    End If
                                                Next
                                            End If
                                        Next
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            End If         
        Next

Solution

  • The easiest way to handle XML in VB.NET is to use XElement instead of XmlDocument and XML literals (at least most of the time).

    So to get all <IoObject> elements, simply use

    Dim xml = XElement.Load("H:\Data.xml")
    
    For Each node In xml...<IoObject>
        MsgBox(node.Value)
    Next