Trouble Binding Data From XML in ASP.NET



I am trying to populate my model with data from an XML file, but have been unsuccessful so far. Setting a breakpoint at the variable Res shows a null value.


I also tried loading the XML file locally, but the outcome was the same.


I am using VS2013, MVC.


Controller:



public ActionResult Index()
{
IQueryable<Restaurant> Res;

XDocument xmlDoc = XDocument.Load
("http://ift.tt/12QHtvT");

var model =
from xml in xmlDoc.Descendants("EstablishmentDetail")
select new Restaurant
{
FHRSID = (int)xml.Element("FHRSID"),
BusinessName = (string)xml.Element("BusinessName"),
RatingValue = (int)xml.Element("RatingValue"),
HygieneScore = (int)xml.Element("Hygiene"),
};

Res = model.AsQueryable();

return View(Res);
}


Model:



[XmlRoot("EstablishmentDetails")]
public class Restaurant
{

public int? RestaurantId { get; set; }

[XmlElement("FHRSID")]
public int FHRSID { get; set; }
[XmlElement("BusinessName")]
public string BusinessName { get; set; }
[XmlElement("RatingValue")]
public int? RatingValue { get; set; }
[XmlElement("Hygiene")]
public int? HygieneScore { get; set; }
}


Sample from the XML file:



<FHRSEstablishment>
<Header>
<ExtractDate>2014-09-19</ExtractDate>
<ItemCount>933</ItemCount>
<ReturnCode>Success</ReturnCode>
</Header>
<EstablishmentCollection>
<EstablishmentDetail>
<FHRSID>129104</FHRSID>
<LocalAuthorityBusinessID>5952</LocalAuthorityBusinessID>
<BusinessName>5 Elm's Cafe</BusinessName>
<RatingValue>3</RatingValue>
<Scores>
<Hygiene>10</Hygiene>
<Structural>10</Structural>
<ConfidenceInManagement>10</ConfidenceInManagement>
</Scores>
</EstablishmentDetail>
</EstablishmentCollection>
</FHRSEstablishment>


I am quite new to web development in general, this is my first time working with XML files.


No comments:

Post a Comment