Link to home
Start Free TrialLog in
Avatar of donhannam
donhannamFlag for New Zealand

asked on

Read XML file with several level

I need to read data from an XML file and am struggling to loop through the different levels.

Program loops through the first Mix record but not subsequent ones.

In this case the XML is in levels - I need information for each Component but also need related info from the mix. I have tried many variations on reading this but carn't get it to loop through correctly. There could be multiple jobs and mixes for each job.

Be glad of any help on this or direction - XML file attached and my vb code below:-

        Dim settings As New XmlReaderSettings
        settings.IgnoreWhitespace = True
        settings.IgnoreComments = True
        Dim xmlIn As XmlReader = XmlReader.Create("Test.xml", settings)
        Dim intCounter As Integer
        Dim strCode As String = ""
        Dim strStkCode As String = ""
        Dim intMixCounter As Integer
        Dim intCompCount As Integer
        Dim strJobNo As String = ""
        Dim dblWeight As Double = 0
        Dim strMixDate As String

        xmlIn.MoveToContent()
        If xmlIn.ReadToDescendant("Job") Then
            Do
                If xmlIn.ReadToDescendant("Mix") Then
                    Do
                        xmlIn.ReadStartElement("Mix")
                        intMixCounter = (xmlIn.ReadElementString("MixCounter"))
                        strCode = (xmlIn.ReadElementString("MixJobNumber"))
                        strMixDate = (xmlIn.ReadElementString("MixedDate"))
                        xmlIn.ReadToFollowing("ComponentCount")
                        intCompCount = (xmlIn.ReadElementString("ComponentCount"))

                        'xmlIn.ReadToFollowing("Components")

                        If xmlIn.ReadToDescendant("Component") Then
                            Do
                                xmlIn.ReadStartElement("Component")
                                intCounter = (xmlIn.ReadElementString("ComponentCounter"))
                                xmlIn.ReadToFollowing("ComponentKG")
                                dblWeight = (xmlIn.ReadElementString("ComponentKG"))
                                'Will write code here using info on each component / mix
                            Loop While xmlIn.ReadToNextSibling("Component")
                        End If
                    Loop While xmlIn.ReadToNextSibling("Mix")
                End If
            Loop While xmlIn.ReadToNextSibling("Job")
        End If
Test3.xml
Avatar of MajorBigDeal
MajorBigDeal
Flag of United States of America image

.Net has extensive libraries for doing this, you don't need to code the loops yourself.  Would you be interested in different approach that the one you are currently using?
Avatar of donhannam

ASKER

Yes if there is a better way of doing this be great if you could point me in the right direction.
SOLUTION
Avatar of MajorBigDeal
MajorBigDeal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should mention that the references are System.Xml and System.Xml.XPath
Thanks for the answers - 2 solutions came through at the same time. I tried one from jyparask as in vb and it worked without any changes so no need to try other one - Thought fairest to split points as I think both solutions do what I want.

Thanks again - I spent a lot of time on this and was going round in circles.
Thanks for splitting - glad you were able to get what you needed.