locked
Load XML From Web Request (RESTful) (Visual Studio 2013 / VB) - Windows Store XAML Apps

    Question

  • I have a working example of using XML when the file is local, but am having issues finding clear guidance on how to work with retrieving the XML from the website and doing the same thing.  How do you load an XML feed from a website into a XElement to work with the data versus having the file locally?

    I am working in Visual Basic not C.  Thanks in advance.

        Private Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Vehicles listed during 1985" & vbCrLf & vbCrLf

            Dim VehicleDataXml As XElement = XElement.Load("Assets/epadatasmall.xml")
            Dim queryData = From VehicleInfo In VehicleDataXml.Elements("vehicle") _
                           Where VehicleInfo.Element("year").Value.Contains("1985") _
                           Select VehicleInfo
            For Each scanResult In queryData
                OutputTextBox.Text &= scanResult.Element("make").Value & " " & scanResult.Element("model").Value & vbCrLf
            Next
        End Sub

    I would like to hook into a feed like the following:

    Dim uri As String = "http://www.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2012/make/bmw/model/3-series?format=xml"


    M. Frenchik Developer At Large


    • Edited by GoliathRulz Monday, June 2, 2014 2:50 PM Clarification on programming language
    Monday, June 2, 2014 2:49 PM

Answers

  • Here's a quick snippet:

            Dim uri As New Uri("http://www.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2012/make/bmw/model/3-series?format=xml")
            Dim httpClient As New Windows.Web.Http.HttpClient()
    
            Try
                Dim xml As String = Await httpClient.GetStringAsync(uri)
            Catch ex As Exception
                ' Catch network errors
            End Try
    

    • Marked as answer by GoliathRulz Tuesday, June 3, 2014 11:13 AM
    Monday, June 2, 2014 6:38 PM
    Owner
  • Boom! That was the ticket. I forgot the Async on the Sub.

    Thanks for the help.   Now I am off and running!  Woot!

        Private Async Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Sample XML Feed" & vbCrLf & vbCrLf
    
            Dim uri As New Uri("http://www.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2012/make/bmw/model/3-series?format=xml")
            Dim httpClient As New Windows.Web.Http.HttpClient()
    
            Try
                Dim xmloutput As String = Await httpClient.GetStringAsync(uri)
                OutputTextBox.Text &= xmloutput
    
            Catch ex As Exception
                ' Catch network errors
                OutputTextBox.Text = "XML Not Loaded - No Internet Connection"
            End Try
    
        End Sub


    M. Frenchik Developer At Large

    • Marked as answer by GoliathRulz Tuesday, June 3, 2014 11:28 AM
    Tuesday, June 3, 2014 11:22 AM

All replies

  • Use HttpClient to download the XML and then pass the resulting stream to XElement.Load
    Monday, June 2, 2014 3:23 PM
    Owner
  • Any chance I can get a code example using the link above?  I'm seeing examples of this in C but not really assembling this myself in VB.  Thanks in advance.

    M. Frenchik Developer At Large

    Monday, June 2, 2014 3:57 PM
  • Start with http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.web.http.httpclient.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

    The C# and VB are almost identical other than the semi colons and the variable declarations. You can also run C# through any of several automated translation suited if you don't remember how to declare variables in VB.

    If you need more specific code please explain which part you have trouble with.

    --Rob on the phone

    Monday, June 2, 2014 4:42 PM
    Owner
  • Here's a quick snippet:

            Dim uri As New Uri("http://www.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2012/make/bmw/model/3-series?format=xml")
            Dim httpClient As New Windows.Web.Http.HttpClient()
    
            Try
                Dim xml As String = Await httpClient.GetStringAsync(uri)
            Catch ex As Exception
                ' Catch network errors
            End Try
    

    • Marked as answer by GoliathRulz Tuesday, June 3, 2014 11:13 AM
    Monday, June 2, 2014 6:38 PM
    Owner
  • Thanks Rob:

    One thing that may be important on this discussion is that I am trying to write a Visual Studio 2013 Express for Windows 8.1, which seems to not have the same library compared to the 2012 for 8 version.  Also, I am creating a Windows 8.1 Store Application using the Basic VB XAML template as the starting point. 

    So, I reviewed the post you did and that was the path I started going down with the conversion of the C over to VB.  The issue is that the Await line doesn't fire to populate the xml string as it throws an error to prevent the build.  Without the Await, the GetStringAsync(uri) throws a warning that conversion at runtime could be an issue ....

    Warning 1 Runtime errors might occur when converting 'Windows.Foundation.IAsyncOperationWithProgress(Of String, Windows.Web.Http.HttpProgress)' to 'String'. C:\Users\Mike\Documents\Visual Studio 2013\Projects\AutoCenter\MainPage.xaml.vb 31 33 AutoCenter

    I reviewed this with some other coders I know and the thing about this is the guidance for VB store applications on this was a little weak for how to do this in 2013.  I think if we figure this out then there will be a bunch of people thankful.  I can't believe that getting a simple xml feed into a VB application is this hard, but hey it is what it is.

    Mike Frenchik


    M. Frenchik Developer At Large


    Tuesday, June 3, 2014 2:21 AM
  • The snippet I gave was cut and pasted from a Windows Store app targeting Windows 8.1. You can tell it targeted Windows 8.1 since it used the new-to-Windows 8.1 Windows.Web.Http.HttpClient() class.

    What error did you get and where? Don't forget that to use Await you need to flag the function as Async (see Quickstart: Calling asynchronous APIs in  C# or Visual Basic ). Understanding asynchronous API is essential to writing Windows Store apps.

    --Rob

    Tuesday, June 3, 2014 2:38 AM
    Owner
  • Boom! That was the ticket. I forgot the Async on the Sub.

    Thanks for the help.   Now I am off and running!  Woot!

        Private Async Sub RunQuery_Click(sender As Object, e As RoutedEventArgs) Handles RunQuery.Click
            OutputTextBox.Text = "Sample XML Feed" & vbCrLf & vbCrLf
    
            Dim uri As New Uri("http://www.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2012/make/bmw/model/3-series?format=xml")
            Dim httpClient As New Windows.Web.Http.HttpClient()
    
            Try
                Dim xmloutput As String = Await httpClient.GetStringAsync(uri)
                OutputTextBox.Text &= xmloutput
    
            Catch ex As Exception
                ' Catch network errors
                OutputTextBox.Text = "XML Not Loaded - No Internet Connection"
            End Try
    
        End Sub


    M. Frenchik Developer At Large

    • Marked as answer by GoliathRulz Tuesday, June 3, 2014 11:28 AM
    Tuesday, June 3, 2014 11:22 AM