I have 2 simple webservice methods that query an XML document (stored in HTTP cache) that I am querying via javascript. The GetCitiesForAffiliate() method is throwing a NullReferenceException on the "select new" line when I pass in one perfectly valid item of data for the aff parameter. It works fine for other data in the aff parameter. The other method also works fine, even with the same aff parameter that causes the other method to bomb out.
I just validated the XML that I am querying. Both methods just return an empty json string when I pass in an aff that doesn't exist, which is OK. What should I look at that might be wrong?
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCitiesForAffiliate(string aff)
{
LocationService loc = new LocationService();
var query = (from center in loc.centersXml.Descendants("Center")
where center.Element("ServiceArea").Value.Equals(aff)
select new {
City = center.Element("City").Value
}).Distinct().OrderBy(x => x.City);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(query);
return json;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCentersForAffiliateCity(string aff, string city)
{
LocationService loc = new LocationService();
var query = (from center in loc.centersXml.Descendants("Center")
where center.Element("ServiceArea").Value.Equals(aff) && center.Element("City").Value.Equals(city)
select new {
ID = center.Element("ID").Value,
Name = center.Element("Name").Value
}).Distinct().OrderBy(x => x.Name);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json = serializer.Serialize(query);
return json;
}
center.Element("City")could be null (meaning there is noCityelement).