我正在尝试使用XML文件中的数据填充我的模型,但到目前为止还没有成功。在变量Res处设置断点显示空值。
我也尝试在本地加载XML文件,但结果是一样的。
我正在使用VS2013,MVC。
控制器:
public ActionResult Index()
{
IQueryable<Restaurant> Res;
XDocument xmlDoc = XDocument.Load
("http://ratings.food.gov.uk/OpenDataFiles%5CFHRS501en-GB.xml");
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);
}
型号:
[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; }
}
XML文件中的示例:
<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>
我对Web开发很新,这是我第一次使用XML文件。
新控制器:
public class RestaurantController : Controller
{
public static IEnumerable<Restaurant> GetData()
{
XDocument xmlDoc = XDocument.Load(@"~/Xml/OpenDataFiles_FHRS501en-GB.xml");
foreach (var xml in xmlDoc.Descendants("EstablishmentDetail"))
{
var eFHRSID = xml.Element("FHRSID");
var eBusinessName = xml.Element("BusinessName");
var eRatingValue = xml.Element("RatingValue");
var eHygieneScore = xml.Element("Scores").Element("Hygiene");
if (eFHRSID != null && eBusinessName != null && eRatingValue != null && eHygieneScore != null)
{
yield return new Restaurant
{
FHRSID = (int)eFHRSID,
BusinessName = (string)eBusinessName,
RatingValue = (int)eRatingValue,
HygieneScore = (int)eHygieneScore,
};
}
}
}
public ActionResult Index()
{
GetData();
return View();
}
答案 0 :(得分:0)
防御性编程。进行空检查或捕获异常。 'Hygiene'元素属于'Scores'所以
您需要xml.Element("Scores").Element("Hygiene")
而不是xml.Element("Hygiene")
。这可能是一个选择:
public static IEnumerable<Restaurant> GetData ( )
{
XDocument xmlDoc = XDocument.Load("http://ratings.food.gov.uk/OpenDataFiles%5CFHRS501en-GB.xml");
foreach (var xml in xmlDoc.Descendants("EstablishmentDetail"))
{
var eFHRSID = xml.Element("FHRSID");
var eBusinessName = xml.Element("BusinessName");
var eRatingValue = xml.Element("RatingValue");
var eHygieneScore = xml.Element("Scores").Element("Hygiene");
if (eFHRSID != null && eBusinessName != null && eRatingValue != null && eHygieneScore != null)
{
yield return new Restaurant
{
FHRSID = (int)eFHRSID,
BusinessName = (string)eBusinessName,
RatingValue = (int)eRatingValue,
HygieneScore = (int)eHygieneScore,
};
}
}
}