0

Visiting this url: "https://api.randomsite.com/api1/1/auth.asp?username=x&password=x"

Should generate this xml if the username and password are correct

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<auth>
    <authentication>successful</authentication> 
    <token>{3FE7472B-E43C-442A-BE6D-2643239F3204}</token> 
    <expires>20110307</expires> 
</auth>

I am trying to access this in VB.Net with this code:

Dim Login As XDocument = XDocument.Load("https://api.randomsite.com/api1/1/auth.asp?username=" + Username.Text + "&password=" + Password.Text)
    Dim ResultofAuth As XElement = Login...<authentication>
    If ResultofAuth = "successful" Then
        Look Happy
    Else
        Look Sad because password probably incorrect!
    End If

But I am generating an error with this:

 Dim Login As XDocument = XDocument.Load("https://api.randomsite.com/api1/1/auth.asp?username=" + Username.Text + "&password=" + Password.Text)

The error says that XDocument.Load cannot be for external xml files. Is there a workaround to use an xml file from the web?

3
  • 1
    Use WebRequest to fetch content from web, and then XDocument.Parse to parse it as XML. Commented Mar 27, 2014 at 21:05
  • Maybe this answer would help you - like Marcin had said - using WebClient. Commented Mar 27, 2014 at 21:08
  • I like to use the Dataset.ReadXML() method and work with it in a datatable. Very easy and works with rest-enabled, password protected sites directly. Commented Mar 27, 2014 at 21:32

1 Answer 1

1

Use a web client to download the stream to the document, then parse the stream into the XDocument class

Dim client As New WebClient()
AddHandler client.OpenReadCompleted,
    Sub(s As Object, e As OpenReadCompletedEventArgs)
        If e.Error Is Nothing Then
            Dim doc = XDocument.Load(e.Result)
            ''TODO: Parse document
        End If
    End Sub
client.OpenReadAsync(New Uri("https://api.randomsite.com/api1/1/auth.asp?username=x&password=x", UriKind.Absolute))
Sign up to request clarification or add additional context in comments.

4 Comments

How do I get the if statement to work?( Dim ResultofAuth As XElement = Login...<authentication> If ResultofAuth = "successful" Then Look Happy Else Look Sad because password probably incorrect! End If)
What does e.Error contain?
Sorry - My poor explanation. How do I dim the contents of the <authentication> tag of the XML file for use later?
I am now getting this error: Lambda parameter e hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.