将 XElement 反序列化为类

发布于 2024-11-15 01:40:05 字数 1609 浏览 2 评论 0原文

我正在尝试将 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; }
}

我当前遇到的问题是,如果有多个艺术家、专辑和/或歌曲,那么它只会填充第一个。我怎样才能做到这一点,以便它填充专辑中的所有内容,或艺术家的所有歌曲......等等我尝试将它们设置为数组,但它不起作用。提前致谢。

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 didn't work. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-11-22 01:40:05

我对你的类进行了一些修改,所以现在 Artist 有一个 List 并且 Album 有 List

我必须稍微修改生成的 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; }
}

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文