2012-05-17 46 views
0

無法嘗試將XDocument轉換爲對象列表,特別是Categories元素。以下是以下列格式返回展示列表的XML片段。Linq to 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> 
} 

每當嘗試顯示類別時,都會出現以下錯誤「未將對象引用設置爲對象的實例」。從我所看到的,每個節目都有一個或多個類別。

任何想法?

回答

0

這就是問題所在:

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

您使用s.Attributes.Value代替c.Attributec.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 
     }), 
+0

哦,我的上帝!我一直盯着這幾個小時,在這個過程中可能還會增加幾個小時。可能必須購買一些Just For Men。謝謝@JonSkeet –

相關問題