将XElement反序列化为类

时间:2011-06-10 20:14:57

标签: c# xml xml-deserialization

我正在尝试将XML文件反序列化为几个类对象:艺术家,专辑和歌曲

以下是当前设置:

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

我目前遇到的问题是,如果有多个艺术家,专辑和/或歌曲,那么它只会填充第一个。我怎样才能让它为所有专辑或艺术家的所有歌曲填充...等我尝试将它们设置为数组,但它没有用。提前谢谢。

1 个答案:

答案 0 :(得分:20)

我稍微修改了你的课程,所以现在艺术家有一个List<Album>而专辑有List<Songs>

我必须稍微修改生成的xml,以确保它正确填充类。这是代码

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

希望有所帮助。这不包括您想要多个艺术家的情况。