0

I've got a function api_request which takes API method as an argument, and returns XMLTextReader

Shared Function api_request(method As String) As XmlTextReader
request_text = method & ".xml"
 url = "https://api.vk.com/method/" & request_text & "&access_token=" & token
 Return New XmlTextReader(url)
End Function

I call this function from different places to make request to site API and depending on the method results are parsed very differently.

So in each method I have something like this:

Dim s As Xml.XmlReader = api_request("users.get")
While s.Read
        If s.NodeType = XmlNodeType.Element Then
          If s.Name = "user" Then
              curr_user=s.ReadElementContentAsString            
          ElseIf s.Name = "error" Then
             error_handler(s, "user.get")
          End If
       End If
End While

As you may see, I have the code ElseIf s.Name = "error" Then error_handler(s, "user.get"). This is because when error happens, server always returns something like this:

<error>
<error_code>4</error_code>
<error_msg>Incorrect signature</error_msg>
</error>

This is parsed in error_handler Sub, and depending on the error following actions are chosen.

This code works, but I have to check if I encounter error like that ElseIf s.Name = "error" every time, though all the methods call the api_request function. Is it possible to check for error in api_request function before returning the Reader? The problem is if I start reading xml there, and there isn't an error, I can't anyhow position the Reader to the start.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Konstantin Pereiaslov
  • 1,786
  • 1
  • 18
  • 26

1 Answers1

1

Since you can't change your position with an XmlTextReader, the only other solution would be to load the entire XML document into memory. Presumably the XmlTextReader is going to download the entire XML file into memory when it first reads it anyway, so any performance hit should be negligible. I would suggest something like this:

Shared Function api_request(ByVal method As String) As XmlDocument
    request_text = method & ".xml"
    url = "https://api.vk.com/method/" & request_text & "&access_token=" & token
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load(New XmlTextReader(url))
    Dim node As XmlNode = doc.SelectSingleNode("error")
    If node IsNot Nothing Then
        Try
            Dim errorCode As Integer = Integer.Parse(node.SelectSingleNode("error_code").InnerText)
            Dim errorMessage As String = node.SelectSingleNode("error_msg").InnerText
            errorHandler(errorCode, errorMessage, method)
            doc = Nothing
        Catch ex As Exception
            Throw New Exception("Improperly formatted error response: " + doc.InnerXml, ex)
        End Try
    End If
    Return doc
End Function

And then in the method that calls api_request, do something like this:

    Dim doc As XmlDocument = api_request("users.get")
    If doc IsNot Nothing Then
        For Each node As XmlNode In doc.SelectNodes("path_to_user/user")
            curr_user = node.InnerText
        Next
    End If
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105