Link to home
Create AccountLog in
Avatar of al4ddin

asked on

read xml, use results to search another XML then build and save the results


I have a windows app that downloads an XML from an API.
What I want to do is use this XML to loop through and store the child values (below)

Then I need to use these values to build a URL to view another XML API that is specific to that record and save a number of elements from that. Once I have looped through the top 5 I would like to then use these to build the results XML and save that to the server so that I can use XSLT to do a transform.

Hope this makes sense.

Avatar of kaufmed
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of al4ddin



thanks for that. I'll give that a try later.
One question I do have though is that the second XML will be a URI link with username and password HTTP authentication i.e.

How will this effect the code?

>>  How will this effect the code?

I don't understand. Are you saying that the 2nd XML will be downloaded on demand from that link, or that the data contained within a particular node within the 2nd XML file will contain a URI?
Avatar of al4ddin


The 2nd XML will be downloaded from the API and the URI will be built dynamically from the results of the first XML.

So would need to load the second XML from the API shown above and then drill through that.

So I can't use 'secondary.Load("secondary.xml")' can I?
What about webclient.openRead and then I can  set the network credentials in webclient too?

Thanks for your help
Avatar of al4ddin


I think Im almost there.

I have this, which successfully accesses the API file, I just need to loop through the results extracting the relevant nodes. Can you help with that? Im not sure how to go from this to looping through it.

Dim initial As New XmlDocument
        Dim secondary As New XmlDocument
        Dim results As New XmlDocument

        Dim proxy As New System.Net.WebProxy("proxy settings")
        proxy.Credentials = CredentialCache.DefaultCredentials
        WebRequest.DefaultWebProxy = proxy

        Dim client As New WebClient()

        ' Create the root node of the results file


            For Each code As XmlNode In initial.SelectNodes("root/code")

                Dim year As XmlNode = initial.SelectSingleNode("root/code/@year")

                client.Credentials = New NetworkCredential("username", "password")
                Dim URL As String = "" & code.InnerText & "/" & year.InnerText & "/"

                Dim strm As Stream = client.OpenRead(URL)
                Dim sr As New StreamReader(strm)

            Label1.Text = "testing"
        Catch ex As Exception
            Label1.Text = ex.ToString()
        End Try

Open in new window

Avatar of al4ddin


Hi kaufmed,

Have you managed to have a look at my code? Really need this working asap.

Thanks for your time.