在VB中解析XML生成错误

时间:2014-03-27 20:59:57

标签: xml vb.net parsing windows-phone-8 xml-parsing

访问此网址:“https://api.randomsite.com/api1/1/auth.asp?username=x&password=x

如果用户名和密码正确,则应生成此xml

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

我试图使用以下代码在VB.Net中访问它:

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

但我生成错误:

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

错误说XDocument.Load不能用于外部xml文件。是否有使用Web上的xml文件的解决方法?

1 个答案:

答案 0 :(得分:1)

使用Web客户端将流下载到文档,然后将流解析为XDocument类

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))
相关问题