Skip to content

Web Service written in ASP.NET C # returning XML

This page provides an example of a web service written in ASP.NET C#. The service supports the following requests:

  • Hotspot with response as XML
  • Data with response as HTML in XML
  • Data with response as XML

Script

The script can be invoked using three different methods to request a hotspot, data as HTML in XML, or data as XML. All three methods create an XML string and call the emitResponse method to create an XML document from the string. The emitResponse method returns the XML as the response to the request.

The difference between the datahtml and dataxml methods is that datahtml emits <data> nodes having HTML as their content. The dataxml method emits <data> nodes having XML as their content. The response from the datahtml method provides HTML to your map's onEventLiveDataResponse handler with having to parse the XML yourself as would be the case when using the dataxml method.

<%@ WebService Language="C#" Class="Example" %>
using System.Web.Services;
using System.Xml;
[WebService(Namespace = "https://mapsalive.com")]

public class Example : System.Web.Services.WebService
{
    [WebMethod]
    public XmlDocument hotspot(string id)
    {
        string html = "Reqest for hotspot " + id + " received";
        string xml = string.Format("<hotspot><html>{0}</html></hotspot>", html);
        return emitResponse(xml);
    }

    [WebMethod]
    public XmlDocument datahtml(string id)
    {
        string xml = "<root>";
        for (int i = 1; i <= 3; i++) 
        {
            string data = id + "-" + i;
            xml += string.Format("<data id='{0}'><div><![CDATA[{1}]]></div></data>", i, data);
        }
        xml += "</root>";
        return emitResponse(xml);
    }

    [WebMethod]
    public XmlDocument dataxml(string id)
    {
        string xml = "<root>";
        for (int i = 1; i <= 3; i++) 
        {
            string data = id + "-" + i;
            string color = "#00000" + i;
            xml += string.Format("<data> id='{0}'<color>{1}</color><html><div><![CDATA[{2}]]></div></html></data>", id, color, data);
        }
        xml += "</root>";
        return emitResponse(xml);
    }

    private XmlDocument emitResponse(string xml)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);
        return xmlDoc;
    }
}

Usage

Below are examples of requests that invoke the above web service called webservice.asmx.

function onEventRequestLiveData(event) {
   let url = "https://mapsalive.com/livedata/asp/example/webservice.asmx/hotspot";
   event.api.liveData.requestHotspot("xml", 0, url, "id",event.hotspot.id);
}
function onEventPageLoading(event) {
   let url = "https://mapsalive.com/livedata/asp/example/webservice.asmx/datahtml";
   event.api.liveData.requestHotspot("xml", 0, url, "id", "test");
}

Response

Below are responses from the web service. You can click the links to see the responses in a browser.

Hotspot HTML as XML

https://mapsalive.com/livedata/asp/example/webservice.asmx/hotspot?id=test

<hotspot>
    <html>Reqest for hotspot 'test' received</html>
</hotspot>

Data as HTML

https://mapsalive.com/livedata/asp/example/webservice.asmx/datahtml?id=test

<root>
    <data id="1"><div><![CDATA[ test-1 ]]></div></data>
    <data id="2"><div><![CDATA[ test-2 ]]></div></data>
    <data id="3"><div><![CDATA[ test-3 ]]></div></data>
</root>

Data as XML

https://mapsalive.com/livedata/asp/example/webservice.asmx/dataxml?id=test

<root>
    <data id="1">
        <color>#000001</color>
        <html><div><![CDATA[ test-1 ]]></div></html>
    </data>
    <data id="2">
        <color>#000002</color><html><div><![CDATA[ test-2 ]]></div></html>
    </data>
    <data id="3">
        <color>#000003</color>
        <html><div><![CDATA[ test-3 ]]></div></html>
    </data>
</root>

Error

https://mapsalive.com/livedata/asp/example/webservice.asmx/hotspot

The web service responds with a server 500 error if the service is not called correctly. MapsAlive detects and reports the cause, for example "Missing parameter: id."