Solved

how to loop through this xml (so that i can put this in a db)

Posted on 2010-11-17
8
740 Views
Last Modified: 2013-11-10
i'm trying to loop through this xml which has nested nodes and attributes
and i'm a bit confused on how to do it successfully

one of the problems i'm having is that once i loop though one layer i'm getting doubles
Data.xml
0
Comment
Question by:pzirkind
8 Comments
 
LVL 3

Expert Comment

by:tomnorra
ID: 34157558
If you are trying to get it into a database why not just use a Stored Procedure to import the file? Assuming of course you are using SQL Server
0
 
LVL 5

Expert Comment

by:srinivasbn
ID: 34157771
Try to read the data to dataset using dataset.readxml. and then just have a look at the data using dataset visualizer. so that you may get some idea about the data. if not please provide me some code to look.
0
 

Author Comment

by:pzirkind
ID: 34158241
here is the code i wrote
the problem is that once i get to the second layer , i'm getting doubles


 Private Sub GetFirstItem(ByVal file_name As String)
        Label1.Text = ""

        Dim xmldoc As New XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As XmlNode
        Dim child As XmlNode



        For Each node In nodelist.Item(0)
            'Label1.Text = Label1.Text & node.Name & vbCrLf

            For Each child In childnodelist.Item(0)

                Label1.Text = Label1.Text & child.Name & vbCrLf
            Next
        Next

    End Sub
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34158633
0
 
LVL 5

Expert Comment

by:srinivasbn
ID: 34158772
Hi have the code modify in the following manner

        Dim xmldoc As New Xml.XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As Xml.XmlNode
        Dim child As Xml.XmlNode

        For Each child In childnodelist.Item(0)
           Label1.Text = Label1.Text & " " & child.Name & vbCrLf
        Next


you just need to get the childnodes. Dont put that for loop in the product nodes loop. this will give only one time the result.

Srinivas.
0
 

Author Comment

by:pzirkind
ID: 34159016
the thing is that i need to grab both (and many more) levels of nodes
0
 
LVL 5

Accepted Solution

by:
srinivasbn earned 250 total points
ID: 34164609
Hi if you want to get the child nodes of a selected node then just have only with that. like the following you can get those.

        Dim xmldoc As New Xml.XmlDocument
        xmldoc.Load(file_name)

        Dim nodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim childnodelist As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product")
        Dim chidnodelist2 As Xml.XmlNodeList = xmldoc.SelectNodes("Result/Product/OtherData")
        Dim node As Xml.XmlNode
        Dim child As Xml.XmlNode

        For Each child In childnodelist.Item(0)
           Label1.Text = Label1.Text & " " & child.Name & vbCrLf
        Next

       For Each child in childnodelist2.Item(0)
          Label2.Text = Label2.Text & " " & child.Name & vbCrLf
      Next

      For Each child in nodelist.Item(0)
         Label3.Text = Label3.Text & " " & child.Name & vbCrLf
      Next


Srinivas.
0
 

Author Comment

by:pzirkind
ID: 34164934
thanks a lot!

i appreciate you putting up with my slow brain
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

840 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