0

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 :)

OmerBTW
  • 780
  • 7
  • 11
  • Consider to show us a mininmal but complete and well-formed sample of the XML document you load, in your current sample `` is not well-formed and it is not clear how your real XML looks, in particular as the code tries to access `Property` elements and their attributes like `Name` or `Image` without being any attributes present on the `Property` element you show. – Martin Honnen Aug 03 '13 at 14:00
  • Here is a link o the XML file: [XML FILE](http://omerbuzo.me/Realestate.xml) – OmerBTW Aug 03 '13 at 16:19

1 Answers1

0

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")
    };
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you so much it works. can you explain please what was the problem? – OmerBTW Aug 03 '13 at 17:16
  • Well the `Property` elements have child elements like `Name` or `Image` and to access them you use `Element("Name")`. `Attribute("Name")` would work for ``. – Martin Honnen Aug 03 '13 at 17:35
  • Didyou cast it, so it will always return a string, even if Element is null? – OmerBTW Aug 03 '13 at 17:42
  • Yes, the LINQ to XML API intentionally implements those casts to allow you deal with `Property` elements which don't have a `Name` child element. If you used `prop.Element("Name").Value` it would throw an error if no `Name` element exists but the `(string)prop.Element("Name")` simply returns `null` and does not cause an error. – Martin Honnen Aug 03 '13 at 17:58
  • OK. You've just cleared it all to me. again thank you for your help. :) – OmerBTW Aug 03 '13 at 17:59