Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize XElement into Class(s)

I am trying to Deserialize an XML file into a few class objects: Artist, Album, and Songs

Here is the current setup:

static void Main(string[] args)
    {
        var riseAgainst = DeSerializer(CreateElement());
        Console.WriteLine(string.Format("Band: {0}",riseAgainst.Name));
        Console.WriteLine("-----------------------------");
        Console.WriteLine(string.Format("Album: {0}",riseAgainst.Album.Name));
        Console.WriteLine("-----------------------------");
        Console.WriteLine("Song List:\r");
        foreach(var s in riseAgainst.Album.Songs)
        {
            Console.WriteLine(string.Format("Song: {0}", s));
        }
        Console.ReadLine();
    }

    static XElement CreateElement()
    {
        return new XElement("Artist",
                new XElement("Name", "Rise Against"),
                new XElement("Album",
                    new XElement("Name", "Appeal to Reason"),
                    new XElement("Songs",
                        new XElement("Song", "Hero of War"),
                        new XElement("Song", "Savior"))
                        )
            );
    }

    static Artist DeSerializer(XElement element)
    {
        var serializer = new XmlSerializer(typeof(Artist));
        return (Artist)serializer.Deserialize(element.CreateReader());
    }
}

public class Artist
{
    public string Name { get; set; }
    public Albums Album { get; set; }
}

public class Albums
{
    public string Name { get; set; }
    public Songs Songs { get; set; }
}

public class Songs
{
    public string Song { get; set; }
}

The problem I currently have is if there is more than one Artist,Album, and/or Song then it only fills the first one. How can i make it so it fills them all for the album, or all the songs for the artist... etc I tried setting them up as Arrays but it didnt work. Thanks in advance.

like image 429
Alex Avatar asked Jun 10 '11 20:06

Alex


1 Answers

I modified your classes a little bit so now Artist has a List<Album> and Album has List<Songs>

I had to modify the generated xml a little bit to make sure it populates the classes properly. Here is the code

static void Main(string[] args)
{
    var riseAgainst = DeSerializer(CreateElement());
        Console.WriteLine(string.Format("Band: {0}",riseAgainst.Name));
        Console.WriteLine("-----------------------------");
        Console.WriteLine(string.Format("Album: {0}",riseAgainst.Albums.First().Name));
        Console.WriteLine("-----------------------------");
        Console.WriteLine("Song List:\r");
        foreach(var s in riseAgainst.Albums.First().Songs)
        {
            Console.WriteLine(string.Format("Song: {0}", s.Name));
        }
        Console.ReadLine();



    static XElement CreateElement()
    {
        return new XElement("Artist",
                new XElement("Name", "Rise Against"),
                new XElement("Albums",
                    new XElement("Album",
                        new XElement("Name", "Appeal to Reason"),
                        new XElement("Songs",
                            new XElement("Song", new XElement("Name", "Hero of War")),
                            new XElement("Song", new XElement("Name", "Savior")))
                        ))
            );
    }

    static Artist DeSerializer(XElement element)
    {
        var serializer = new XmlSerializer(typeof(Artist));
        return (Artist)serializer.Deserialize(element.CreateReader());
    }
}

public class Artist
{
    public string Name { get; set; }
    public List<Album> Albums { get; set; }
}

public class Album
{
    public string Name { get; set; }
    public List<Song> Songs { get; set; }
}

public class Song
{
    public string Name { get; set; }
}

Hope that helps. This doesn't cover the case where you want more than one artist.

like image 50
marto Avatar answered Oct 17 '22 09:10

marto