Link to home
Start Free TrialLog in
Avatar of ssj1
ssj1

asked on

Iterating xml node

I am trying to parse thru this xml. I am having a problem in iterating through multple "adins" node. (See below example) I am not able to go to the next "adins" node. The "adins" occurs multiple times. I am able to use the nextsibling property. But how can I go to the 3rd. Below is the code
Thank you for the help.
 
I am using in Visual basic 2010 (.net).
    xmlNode = xmlDoc.GetElementsByTagName("adins")
            singlexmlNode = xmlNode(I)
            Dim xmlInsertNode1 As XmlNode
            Dim xmlInsertNode2 As XmlNode
            k = 0
            For Each xmlInsertNode1 In singlexmlNode.ChildNodes
                If xmlInsertNode1.HasChildNodes Then
                    For Each xmlInsertNode2 In xmlInsertNode1.ChildNodes
                          MsgBox("cost: " & xmlInsertNode2.ChildNodes.Item(1).InnerText.Trim())
                         MsgBox("DATE: " & xmlInsertNode2.ChildNodes.Item(0).InnerText.Trim())
                        End If
                      ' Tried using child nodes but doesn't work.
                        drCurrent("date") = xmlNode(I).ChildNodes.Item(0).InnerText.Trim()
                        drCurrent("price") = xmlNode(I).ChildNodes.Item(1).InnerText.Trim()
                        'code to update data base
                    Next
                End If
           next
 
 
   'example layout of xml
 <Orders>
 <ord>
     <item1>sval 1</item1>
     <item2>sval 1</item2>
     <item3>sval 1</item3>
     <Accountinfo>
            <CustomerNumber>10059</CustomerNumber>
            <Name>test name</Name>
            <Company>company test</Company>
            <Contact>
               <PhoneNumber>222-2222</PhoneNumber>
               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber>
               <Street>test add</Street>
            </Contact>
      </Accountinfo>
      <adins>
            <date>2011-01-01</date>
             <adcost>2200.00</adcost>
             <status>Active</status>
      </adins>
       <adins>
             <date>2011-01-02</date>
              <adcost>3200.00</adcost>
              <status>Active</status>
         </adins>
         <adins>
             <date>2011-01-03</date>
              <adcost>4200.00</adcost>
              <status>Active</status>
         </adins>
    </ord>
Avatar of pivar
pivar
Flag of Sweden image

Hi,

How about this?


/peter

For Each node As XmlNode In doc.SelectNodes("/Orders/ord/adins")
	For Each subnode As XmlNode In node.ChildNodes
		If subnode.Name.Equals("date") Then
			drCurrent("date") = subnode.InnerText
		End If
		If subnode.Name.Equals("adcost") Then
			drCurrent("price") = subnode.InnerText
		End If
		If subnode.Name.Equals("Status") Then
			drCurrent("status") = subnode.InnerText
		End If
	Next
Next

Open in new window

Avatar of ssj1
ssj1

ASKER

Hello,
Thank you for the comments. It is helpfull.
It still goes thru only one set of "adins" nodes and sub nodes. It doesnt go to the next at all.
I dont know what I need to change in "            For Each node In singlexmlNode"

I tried all. I am new to xml scrirpting. Please please help!!!! Thank you


     
Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim singlexmlNode As XmlNode
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")
        singlexmlNode = xmlNode(I) 

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In singlexmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

Why do you change

For Each node As XmlNode In doc.SelectNodes("/Orders/ord/adins")

to
 
xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")
singlexmlNode = xmlNode(I)

For Each node In singlexmlNode

that will only take the first xmlnode.

Please try my solution

Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord/adins")

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In xmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

Avatar of ssj1

ASKER

Pivar:
Thank you for the help. It goes through the complete loop. Basically it goes through all adins nodes for all the orders (not for that specific order). That's why I ended up doing with (I).  I need  <adins> nodes only for the particlar order number for each order.
For each order there will be one or multiple (3 to 10)  <adins> nodes.

Please help. I really need this. Thanks a lot again.
Now I dont understand. You have 3 adin childnodes for the specified "ord" node. Dont you want all 3 childnodes?
Avatar of ssj1

ASKER

Yes, I need all adins nodes pertaining  to that order <ord>.  

(ie) One <ord> that is one order will have multple ads. If I use the code you suggested, it goes through all the <adins>nodes in the entire xml file. It doesnt stop when the <ord> order number changes.

Thank you please help
OK, I think I understand.

How about this? This will select the first occurence of ord (you could change this by changing order in [1])
Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNodeList
        Dim fs As New FileStream("C:\orders0206.xml", FileMode.Open, FileAccess.Read)
        xmlDoc.Load(fs)
        xmlNode = xmlDoc.SelectNodes("/Orders/ord[1]/adins")

         ' "I" for the current iteration thru which I get other fields using xml dataset 

            For Each node In xmlNode

                For Each subnode In node.ChildNodes

                        MsgBox(" chk " & subnode.InnerText)

                        If subnode.Name.Equals("date") Then
                            drCurrent("date") = subnode.InnerText
                        End If
                        If subnode.Name.Equals("adcost") Then
                         drCurrent("price") = subnode.InnerText
                Next
            Next \
' adins loop comes out after this...

Open in new window

Avatar of ssj1

ASKER

No, it doesnt go to the next adins within  that <ORD>. It picks up the first node only. After the loop it comes out, as though only one <adins> is present.

Please help. Sorry to be a pain.....
ASKER CERTIFIED SOLUTION
Avatar of pivar
pivar
Flag of Sweden 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
Avatar of kaufmed
It seems to me you just need one more loop to loop through the "adins" nodes you grabbed and stored in xmlNode:
xmlNode = xmlDoc.GetElementsByTagName("adins")

For Each adinNode As XmlNode In xmlNode
	For Each xmlInsertNode1 In adinNode.ChildNodes
		For Each xmlInsertNode2 In xmlInsertNode1.ChildNodes
			MsgBox("cost: " & xmlInsertNode2.InnerText.Trim())
			MsgBox("DATE: " & xmlInsertNode2.InnerText.Trim())
		Next
	Next
Next

Open in new window

Isn't this a duplicate of the question you already have open here:  https://www.experts-exchange.com/questions/26826303/iterating-xml-child-nodes.html ?
Avatar of ssj1

ASKER

Pivar:
Thank you It worked. I tested it now.  It worked in the test xml and also in the actual xml.
the problem was I thought it the iteration starts from 0, but actually it starts at 1.

The other code I use with xml data set starts at 0.  
thank you for you help