2 Replies - 1359 Views - Last Post: 24 April 2013 - 04:48 AM Rate Topic: -----

#1 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Problem to read json string due to xml tags

Posted 23 April 2013 - 02:09 PM

I am new to using JSon and Ajax in ASP.NET. In fact I think I don't really know what I'm doing! Please follow the next code to understand my problem. I have a requirement to use the Ajax function I will show below, and to use JSon to upload data from an SQL server database.

Loading a JSon object from DB using LINQ:

public JObject GetByAccount(string user)
        {
            Account myAccount = new AccountDAL().GetByID(user);

            var markers = myAccount.Markers.AsQueryable(); //Gets a list of markers for the given user

            JObject json = new JObject(
                new JProperty("markers",
                    new JArray(
                        from m in markers
                        select new JObject(
                        new JProperty("lon", m.markerLong),
                        new JProperty("lat", m.markerLat),
                        new JProperty("title", m.markerTitle),
                        new JProperty("desc", m.markerDesc),
                        new JProperty("url", m.markerIcon)))));

            return json;
        }


I am calling the function above from a web service, converting JSon object into a string:
        [WebMethod]
        public string GetMarkers()
        {
            return new MarkerDAL().GetByAccount(Context.User.Identity.Name).ToString();
        }


and I am using the following ajax function to get the JSon string:

function GetMarkers() {

            if (window.XMLHttpRequest) { 
                var req = new XMLHttpRequest();
            } else {
                var req = new ActiveXObject("Microsoft.XMLHTTP"); 
            }

            req.onreadystatechange = function () {

                if (req.readyState == 4) {

                    alert(req.responseText);
                    var markerObjects = JSON.parse(req.responseText);
                    var markers = markerObjects.markers;

                    for (i = 0; i < markers.length; i++) {

                        var size = new OpenLayers.Size(21, 25);
                        var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
                        url = markers[i].url;
                        var icon = new OpenLayers.Icon(url, size, offset);

                        markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(markers[i].lon, markers[i].lat).transform(
					        new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), icon));
                    }
                }
            };


I am having an error in this AJAX function as I cannot find away to remove the XML tags from the string returned by the webservice function. I'm surprised to how hard I am finding this to solve. I tried changing content type, added attributes to the webservice function and configure httphandler in web.config. But it seems I cannot find the right combination to make this work.

The returning string has the following tags which I need to remove:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">

using this: req.responseText.replace(/<([a-zA-Z0-9 ]+)(?:xml)ns=\".*\"(.*)>/g, "<$1$2>") regex function I managed to change the last tag into <string>, but this is still no good.

Can someone please provide me with a solution?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem to read json string due to xml tags

#2 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Problem to read json string due to xml tags

Posted 23 April 2013 - 11:56 PM

help!
Was This Post Helpful? -1
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Problem to read json string due to xml tags

Posted 24 April 2013 - 04:48 AM

you should really consider looking into using a library like jquery for handling your ajax calls as it would greatly simplify your life. You could simply do:

$.getJSON(url, data, function(response){
   // Actions to take on the returned response goes here.
   // data should be an object containing any information
   // that needs to be sent to the server, ie. {name: "John"}
});



Also what version of .net are you using?

This post has been edited by Nakor: 24 April 2013 - 04:50 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1