在ASP.NET中从XML绑定数据时出现问题

本文关键字:问题 数据 绑定 ASP NET 中从 XML | 更新日期: 2023-09-27 18:27:54

我正试图用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();
    }

在ASP.NET中从XML绑定数据时出现问题

防御性编程。执行空检查或捕获异常。"卫生"元素在"分数"下,因此您需要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,
      };
    }
  }
}