Avatar of donhannam
donhannam
Flag 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
.NET ProgrammingXMLVisual Basic.NET

Avatar of undefined
Last Comment
MajorBigDeal

8/22/2022 - Mon
MajorBigDeal

.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?
donhannam

ASKER
Yes if there is a better way of doing this be great if you could point me in the right direction.
SOLUTION
MajorBigDeal

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
MajorBigDeal

I should mention that the references are System.Xml and System.Xml.XPath
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
donhannam

ASKER
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.
MajorBigDeal

Thanks for splitting - glad you were able to get what you needed.