English 中文(简体)
Linq to Xml - 不针对物体的事例提出反对
原标题:Linq to Xml - Object reference not set to an instance of an object

试图将X文件列入物体清单,特别是类别要素。 以下是XML的幻灯,以以下格式填写一份显示清单。

<Show>
  <Name>OLYMPIC GAMES</Name>
  <Artist>OLYMPIC GAMES</Artist>
  <OnSale>false</OnSale>
  <DateOnSale>2011-03-11T00:00:00</DateOnSale>
  <DoorsOpen>2012-12-31T10:00:00</DoorsOpen>
  <Starts>2012-12-31T10:00:00</Starts>
  <BespokeDate>25 July 2012 - 12 August 2012</BespokeDate>
  <Status Code="3">SOLD OUT</Status>
  <Categories>
    <Category Id="190">OTHER</Category>
  </Categories>
  <Prices>
    <Price Type="1">
      <Status Code="24">ORDER</Status>
      <FaceValue>0.00</FaceValue>
      <BookingFee>0.00</BookingFee>
      <TicketPrice>0.00</TicketPrice>
      <Description>UNRESERVED</Description>
    </Price>
  </Prices>
  <Venue Uri="/venues/">
    <Name>Various venues</Name>
    <Town></Town>
  </Venue>
</Show>

以下是我为模拟XML数据而设立的类别。

public class Show {
  public string Name { get; set; }
  public string Artist { get; set; }
  public bool OnSale { get; set; }
  public DateTime DateOnSale { get; set; }
  public DateTime DoorsOpen { get; set; }
  public DateTime Starts { get; set; }
  public string BespokeDate { get; set; }
  public Status Status { get; set; }
  public IEnumerable<Category> Categories { get; set; }
  public Venue Venue { get; set; }
}

public class Status
{
  public int Code { get; set; }
  public string Description { get; set; }
}

public class Category
{
  public int Id { get; set; }
  public string Name { get; set; }
}

public class Venue
{
  public string Uri { get; set; }
  public string Name { get; set; }
  public string Town { get; set; }
}

以下是制作展品的Linq至XML。

var shows = from s in xdoc.Descendants("Show")
  where 
    s.Element("OnSale").Value.AsBool() != false
  select
    new Show {
      Name = s.Element("Name").Value,
      Artist = s.Element("Artist").Value,
      OnSale = s.Element("Name").Value.AsBool(),
      DateOnSale = s.Element("DateOnSale").Value.AsDateTime(),
      DoorsOpen = s.Element("DoorsOpen").Value.AsDateTime(),
      Starts = s.Element("Starts").Value.AsDateTime(),
      BespokeDate = s.Element("BespokeDate").Value,
      Status = new Status { 
        Code = s.Element("Status").Attribute("Code").Value.AsInt(),
        Description = s.Element("Status").Value
      },
      Categories = (from c in s.Element("Categories").Elements("Category")
        select new Category { 
            Id = s.Attribute("Id").Value.AsInt(),
            Name = s.Value
          }),
      Venue = new Venue {
        Name = s.Element("Venue").Element("Name").Value,
        Town = s.Element("Venue").Element("Town").Value
      }
};

以下是展示展品的密码。

foreach(var show in shows)
{
  <p>
    Name: @show.Name | 
    Status: @show.Status.Description | 
    Venue: @show.Venue.Name | 
    Categories: @string.Join(",", show.Categories.Select(x => x.Name))
  </p>
}

下述错误在试图显示类别时即为“标的不指标的”。 从我可以看到的情况来看,每一次显示都有一个或多个类别。

任何想法?

最佳回答

This is the problem:

  Categories = (from c in s.Element("Categories").Elements("Category")
    select new Category { 
        Id = s.Attribute("Id").Value.AsInt(),
        Name = s.Value
      }),

You re using s.Attribute and s.Value instead of c.Attribute and c.Value.

我也建议使用明确转换为<代码>int而不是Value.AsInt():

  Categories = (from c in s.Element("Categories").Elements("Category")
    select new Category { 
        Id = (int) c.Attribute("Id"),
        Name = (string) c.Value
      }),
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签