I am using the following function to receive XML data from a URL.

Is there a more efficient method for retrieving this XML data?

Public Function UserLocation() As String


        Dim XML As Object = Server.CreateObject("MSXML2.DOMDocument.6.0")
        Dim getXML As String = "<URL>"
        XML.async = "false"
        XML.resolveExternals = "false"
        XML.setProperty("ServerHTTPRequest", True)

        Return XML.documentElement.SelectSingleNode("name").Text

    Catch e As Exception

        Return False

    End Try

End Function
  • \$\begingroup\$ Welcome to Code Review! I hope you get some great answers. \$\endgroup\$ – Phrancis Aug 22 '16 at 1:54
  • \$\begingroup\$ as for me, it's an awful way to work with XML \$\endgroup\$ – Disappointed Aug 22 '16 at 10:30
  • \$\begingroup\$ What exactly do you mean by efficient? In terms of code size, this is already efficient. How can you ever return a boolean from a function that is declared to return a string? \$\endgroup\$ – Roland Illig Jul 3 '17 at 6:56

I am giving code feedback based on c# however I think you can apply that to vb.

You can leverage the HttpWebRequest class to extract the xml response.

public static XmlDocument MakeRequest(string requestUrl)
        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;    
        XmlDocument xmlDoc = new XmlDocument();
        return (xmlDoc);    
    catch (Exception e)
        return null;

Then you can invoke the function with requested url which has xml response.

XmlDocument locationsResponse = MakeRequest(locationsRequest);

Now in the ProcessResponse function you can extract the value from XmlDocument using either XPath query or linq to xml.

 static public void ProcessResponse(XmlDocument doc)
       // Here you query the doc..           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.