Solved

Iterating xml node

Posted on 2011-02-16
12
1,105 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 500 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 74

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 74

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now