Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:43:03+00:00 2026-06-04T00:43:03+00:00

Having trouble trying to turn an XDocument into an list of objects, in particular

  • 0

Having trouble trying to turn an XDocument into an list of objects, in particular the Categories element. Below is a snippet of the XML which returns a list of shows in the following format.

<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>

Below is the classes I have created to model the XML data

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; }
}

Below is the Linq to XML to create the Show objects

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
      }
};

Below is the code snippet to display a Show

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>
}

The following error appears “Object reference not set to an instance of an object”, whenever attempting to display categories. From what I can see, every show has 1 or more categories.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:43:04+00:00Added an answer on June 4, 2026 at 12:43 am

    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.

    I would also suggest using the explicit conversion to int instead of Value.AsInt():

      Categories = (from c in s.Element("Categories").Elements("Category")
        select new Category { 
            Id = (int) c.Attribute("Id"),
            Name = (string) c.Value
          }),
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble trying to represent the below SQL (which returns the results
I'm having trouble trying to convert a text file into a list of lists
I am having trouble trying to connect to a secure OpenLDAP server which I
I'm having trouble trying to properly insert additional code into the onclick attribute of
I am having trouble trying to hard encode a text document into my C#
I'm having trouble trying to build an array in PHP which will output in
I'm having trouble trying to implement a linked List without using classes(we're not there
I'm having trouble trying to put a ProgressDialog into my app. In my GameEngine
I'm having trouble trying to update my xml file with a new value. I
I am trying to turn my array into an arraylist and I am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.