Solved

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

Posted on 2010-11-17
8
737 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 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