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

  • Home
  • SEARCH
  • 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 3941958
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:36:18+00:00 2026-05-20T00:36:18+00:00

I have two classes – Cd and Track which I’m trying to read into

  • 0

I have two classes – Cd and Track which I’m trying to read into from a XML file using Linq.

public Cd(string t, string a, string cat, DateTime rls, Track[] tr)
public Track(string t, int l)

The XML looks like this.

<media>
  <cd>
    <artist>Ozzy Osbourne</artist>
    <album Name="Bark at the moon" Type="Metal" Tracks="5" ReleaseDate="1983-12-10">
      <track Length="300">Bark at the moon</track>
      <track Length="235">You're No Different</track>
      <track Length="567">Now You See It (Now You Don't)</track>
      <track Length="356">Rock 'N' Roll Rebel</track>
      <track Length="120">Centre of Eternity</track>
    </album>
  </cd>
  <cd>
    <artist>Journey</artist>
    <album Name="Escape" Type="Rock" Tracks="4" ReleaseDate="1981-07-31">
      <track Length="300">Don't Stop Believin'</track>
      <track Length="235">Stone in Love</track>
      <track Length="567">Who's Crying Now</track>
      <track Length="356">Keep on Runnin'</track>
    </album>
  </cd>
</media>

The code I’m trying to use is as follows

XElement xdoc = XElement.Load("dbxml.xml");
    var temp = from cds in xdoc.Descendants("cd")
    select new Cd(
        cds.Element("artist").Value,
        cds.Element("album").Attribute("Name").Value,
        cds.Element("album").Attribute("Type").Value,
        DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value),
        new Track[] { // One Track reads fine..
            new Track(cds.Element("album").Element("track").Value,
               int.Parse(cds.Element("album").Element("track").Attribute("Length").Value)) 
               }
        );

The problem is that I don’t know how to initialize the array with all the tracks read from the XML file. I could wrap the whole query in a .ToList(), use an anonomyous type and foreach-it but I’d like to know if there is a way to do this in just one run with linq.

cds.Elements("album").Elements("song") returns an IEnumerable<XElement> collection and somehow that should be added to the array as a range and turned into a string and an int, or something to that affect. Any help out there?

Thanks!

  • 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-05-20T00:36:19+00:00Added an answer on May 20, 2026 at 12:36 am

    This would work:

    XElement xdoc = XElement.Load("test.xml");
    var temp = from cds in xdoc.Descendants("cd")
                select new Cd(
                    cds.Element("artist").Value,
                    cds.Element("album").Attribute("Name").Value,
                    cds.Element("album").Attribute("Type").Value,
                    DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value),
                    cds.Element("album").Descendants("track").Select(t => new Track(t.Value, int.Parse(t.Attribute("Length").Value))).ToArray() 
                    );
    

    Easier to read (imo) using properties:

    select new Cd()
        {
            Artist = cds.Element("artist").Value,
            Album = cds.Element("album").Attribute("Name").Value,
            Type = cds.Element("album").Attribute("Type").Value,
            ReleaseDate = DateTime.Parse(cds.Element("album").Attribute("ReleaseDate").Value),
            Tracks = cds.Element("album")
                        .Descendants("track")
                        .Select(t => new Track()
                        {
                          Name = t.Value,
                          Length = int.Parse(t.Attribute("Length").Value)
                        }).ToArray()
        };
    

    You should consider adding a default constructor to Cd and Track and exposing public properties or use named parameters – this would make the LINQ statement much more readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes which need to be in same xml file. The way
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes which both derive from the same parent: public class People{
I have two classes namely classA and classB which both inherit from class1 .
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes, one that inherits from the other. The base class is
I have two classes Teacher and Student . I am trying to get teacher
I have two classes(Book,Software) that inherit from the Product class. these two classes have
I have two classes. UserModel and UserController. file: localhost/controllers/user.controller.php <?php class UserController { public

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.