Question

I have a generic handler that will read an XML file, and send the result to the ajax call as JSON. When I run the program i get this error:

Client Side Code:

$(function () {
$('#getData').click(function () {
    $.ajax({
        type: 'GET',
        datatype: 'json',
        url: 'DynamicHandler.ashx',
        contentType: "application/json; charset=utf-8",

        success: function (result) {

            var property = JSON.parse(result);

            console.log(property);
        }
    });
});

});

Server Side Code:(Handler.ashx)

  public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    var realestate = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("Realestate.xml"));
    var query = from items in realestate.Descendants("Property")
        select new
        {
            Name = items.Attribute("Name").Value,
            Image = items.Attribute("Image").Value,
            Location = items.Attribute("Location").Value,
            Rooms = items.Attribute("Rooms").Value,
            PropertyValue = items.Attribute("PropertyValue").Value,
            Contact = items.Attribute("Contact").Value,
            Description = items.Attribute("Description").Value
        };


    var scriptSerializer = new JavaScriptSerializer();
    context.Response.Write(scriptSerializer.Serialize(query));
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

}

Link to the XML File:

[http://omerbuzo.me/Realestate.xml][1]

When i run this with debugger i get the following error(on the line of: select new {anonymous object}) in the Handler.ashx file;

Object reference not set to an instance of an object.

and in the console.log i get:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 

Can anyone point what seems to be the problem?

Thank you in advanced :)

Was it helpful?

Solution

Change

var query = from items in realestate.Descendants("Property")
    select new
    {
        Name = items.Attribute("Name").Value,
        Image = items.Attribute("Image").Value,
        Location = items.Attribute("Location").Value,
        Rooms = items.Attribute("Rooms").Value,
        PropertyValue = items.Attribute("PropertyValue").Value,
        Contact = items.Attribute("Contact").Value,
        Description = items.Attribute("Description").Value
    };

to

var query = from prop in realestate.Descendants("Property")
    select new
    {
        Name = (string)prop.Element("Name"),
        Image = (string)prop.Element("Image"),
        Location = (string)prop.Element("Location"),
        Rooms = (string)prop.Element("Rooms"),
        PropertyValue = (string)prop.Element("PropertyValue"),
        Contact = (string)prop.Element("Contact"),
        Description = (string)prop.Element("Description")
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top