Search code examples
c#asp.net-mvc-4razorxmlhttprequestwebservices.nl

How do I open XML from link in razor?


The task is quite simple, connect to another webservice using XML.
In the current pages (classic ASP) we use the following code:

zoekpcode=UCASE(Request.Querystring("zoekpcode")) <-- postal-code
zoeknr=Request.Querystring("zoeknr") <-- house-number

PC=Trim(Replace(zoekpcode," ",""))
NR=Trim(Replace(zoeknr," ",""))

strGetAddress="https://ws1.webservices.nl/rpc/get-simplexml/addressReeksPostcodeSearch/*~*/*~*/" & PC & NR

set xml = Server.CreateObject("Microsoft.XMLHTTP")
xml.open "GET", strGetAddress , false
xml.send ""
strStatus = xml.Status
If Len(PC)>5 and Len(NR)>0 Then
    strRetval = Trim(xml.responseText)
End If

set xml = nothing

'Do something with the result string

One of the possible links could be: https://ws1.webservices.nl/rpc/get-simplexml/addressReeksPostcodeSearch/~/~/1097ZD49

Currently I'm looking for a way to do this in razor (C#), but all I seem to be able to find on Google is how to do it in JavaScript
I've tried (most combinations of) the following terms:

  • razor
  • xmlhttp
  • comobject
  • XML from url
  • -javascript

Results were mostly about JavaScript or razorblades.
Based on other result (like in the search comobjects in razor) it seems that comobject aren't available in Razor.

I did find this question (How to use XML with WebMatrix razor (C#)) on stackoverflow that seems to answer my question (partially), but is it also possible with a link to an external system (the mentioned web-service)?


Solution

  • I have covered the consumption of Web Services in Razor web pages here: http://www.mikesdotnetting.com/Article/209/Consuming-Feeds-And-Web-Services-In-Razor-Web-Pages.

    If your web service is a SOAP one, you are best off using Visual Studio (the free Express editions is fine) to add a service reference and then work from there. Otherwise you can use Linq To XML to load the XML directly into an XDocument as in the ATOM example in the article:

    var xml = XDoxument.Load("https://ws1.webservices.nl/rpc/get-simplexml/blah/blah");
    

    Then use the System.Xml.Linq APIs to query the document.