将XElement反序列化为类 [英] Deserialize XElement into Class(s)

查看:114
本文介绍了将XElement反序列化为类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将XML文件反序列化为一些类对象:艺术家,专辑和歌曲

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

这是当前设置:

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.

推荐答案

我稍微修改了您的课程,所以现在Artist具有 List< Album> 和Album具有 List< Songs>

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

我不得不稍微修改一下生成的xml,以确保它正确地填充了类.这是代码

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.

这篇关于将XElement反序列化为类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆