donhannam
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("Jo b") Then
Do
If xmlIn.ReadToDescendant("Mi x") Then
Do
xmlIn.ReadStartElement("Mi x")
intMixCounter = (xmlIn.ReadElementString(" MixCounter "))
strCode = (xmlIn.ReadElementString(" MixJobNumb er"))
strMixDate = (xmlIn.ReadElementString(" MixedDate" ))
xmlIn.ReadToFollowing("Com ponentCoun t")
intCompCount = (xmlIn.ReadElementString(" ComponentC ount"))
'xmlIn.ReadToFollowing("Co mponents")
If xmlIn.ReadToDescendant("Co mponent") Then
Do
xmlIn.ReadStartElement("Co mponent")
intCounter = (xmlIn.ReadElementString(" ComponentC ounter"))
xmlIn.ReadToFollowing("Com ponentKG")
dblWeight = (xmlIn.ReadElementString(" ComponentK G"))
'Will write code here using info on each component / mix
Loop While xmlIn.ReadToNextSibling("C omponent")
End If
Loop While xmlIn.ReadToNextSibling("M ix")
End If
Loop While xmlIn.ReadToNextSibling("J ob")
End If
Test3.xml
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
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("Jo
Do
If xmlIn.ReadToDescendant("Mi
Do
xmlIn.ReadStartElement("Mi
intMixCounter = (xmlIn.ReadElementString("
strCode = (xmlIn.ReadElementString("
strMixDate = (xmlIn.ReadElementString("
xmlIn.ReadToFollowing("Com
intCompCount = (xmlIn.ReadElementString("
'xmlIn.ReadToFollowing("Co
If xmlIn.ReadToDescendant("Co
Do
xmlIn.ReadStartElement("Co
intCounter = (xmlIn.ReadElementString("
xmlIn.ReadToFollowing("Com
dblWeight = (xmlIn.ReadElementString("
'Will write code here using info on each component / mix
Loop While xmlIn.ReadToNextSibling("C
End If
Loop While xmlIn.ReadToNextSibling("M
End If
Loop While xmlIn.ReadToNextSibling("J
End If
Test3.xml
.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?
ASKER
Yes if there is a better way of doing this be great if you could point me in the right direction.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I should mention that the references are System.Xml and System.Xml.XPath
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.
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.