Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Iterating xml node

Posted on 2011-02-16
12
Medium Priority
?
1,120 Views
Last Modified: 2012-05-11
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>
0
Comment
Question by:ssj1
  • 5
  • 5
  • 2
12 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 34912029
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

0
 

Author Comment

by:ssj1
ID: 34917531
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

0
 
LVL 22

Expert Comment

by:pivar
ID: 34920755
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ssj1
ID: 34921404
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.
0
 
LVL 22

Expert Comment

by:pivar
ID: 34921455
Now I dont understand. You have 3 adin childnodes for the specified "ord" node. Dont you want all 3 childnodes?
0
 

Author Comment

by:ssj1
ID: 34921619
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
0
 
LVL 22

Expert Comment

by:pivar
ID: 34921719
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

0
 

Author Comment

by:ssj1
ID: 34921897
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.....
0
 
LVL 22

Accepted Solution

by:
pivar earned 2000 total points
ID: 34921987
Please try attached code. It works well for me. Have I got the correct xml structure?
Imports System
Imports System.Xml
Imports System.IO

Module Module1

    Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<Orders>" & vbCr & vbLf & " <ord>" & vbCr & vbLf & "     <item1>sval 1</item1> " & vbCr & vbLf & "     <item2>sval 1</item2> " & vbCr & vbLf & "     <item3>sval 1</item3> " & vbCr & vbLf & "     <Accountinfo>" & vbCr & vbLf & "            <CustomerNumber>10059</CustomerNumber> " & vbCr & vbLf & "            <Name>test name</Name> " & vbCr & vbLf & "            <Company>company test</Company> " & vbCr & vbLf & "            <Contact>" & vbCr & vbLf & "               <PhoneNumber>222-2222</PhoneNumber> " & vbCr & vbLf & "               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber> " & vbCr & vbLf & "               <Street>test add</Street> " & vbCr & vbLf & "            </Contact>" & vbCr & vbLf & "      </Accountinfo>" & vbCr & vbLf & "      <adins>" & vbCr & vbLf & "            <date>2011-01-01</date> " & vbCr & vbLf & "             <adcost>2200.00</adcost> " & vbCr & vbLf & "             <status>Active</status> " & vbCr & vbLf & "      </adins>" & vbCr & vbLf & "       <adins>" & vbCr & vbLf & "             <date>2011-01-02</date> " & vbCr & vbLf & "              <adcost>3200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "         <adins>" & vbCr & vbLf & "             <date>2011-01-03</date> " & vbCr & vbLf & "              <adcost>4200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "    </ord>" & vbCr & vbLf & " <ord>" & vbCr & vbLf & "     <item1>sval 1</item1> " & vbCr & vbLf & "     <item2>sval 1</item2> " & vbCr & vbLf & "     <item3>sval 1</item3> " & vbCr & vbLf & "     <Accountinfo>" & vbCr & vbLf & "            <CustomerNumber>10059</CustomerNumber> " & vbCr & vbLf & "            <Name>test name</Name> " & vbCr & vbLf & "            <Company>company test</Company> " & vbCr & vbLf & "            <Contact>" & vbCr & vbLf & "               <PhoneNumber>222-2222</PhoneNumber> " & vbCr & vbLf & "               <BillingPhoneNumber>333-533-3333</BillingPhoneNumber> " & vbCr & vbLf & "               <Street>test add</Street> " & vbCr & vbLf & "            </Contact>" & vbCr & vbLf & "      </Accountinfo>" & vbCr & vbLf & "      <adins>" & vbCr & vbLf & "            <date>2011-02-01</date> " & vbCr & vbLf & "             <adcost>2200.00</adcost> " & vbCr & vbLf & "             <status>Active</status> " & vbCr & vbLf & "      </adins>" & vbCr & vbLf & "       <adins>" & vbCr & vbLf & "             <date>2011-02-02</date> " & vbCr & vbLf & "              <adcost>3200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "         <adins>" & vbCr & vbLf & "             <date>2011-02-03</date> " & vbCr & vbLf & "              <adcost>4200.00</adcost> " & vbCr & vbLf & "              <status>Active</status> " & vbCr & vbLf & "         </adins>" & vbCr & vbLf & "    </ord>" & vbCr & vbLf & "</Orders>")


        Dim nodes As XmlNodeList = doc.SelectNodes("/Orders/ord[1]/adins")

        For Each node As XmlNode In nodes
            For Each subnode As XmlNode In node.ChildNodes
                Console.WriteLine(String.Format("{0}={1}", subnode.Name, subnode.InnerText))
            Next
        Next

        Console.ReadKey()
    End Sub
End Module

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34926478
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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34926545
Isn't this a duplicate of the question you already have open here:  http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_26826303.html ?
0
 

Author Comment

by:ssj1
ID: 34931329
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question