将 XElement 反序列化为类
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你的类进行了一些修改,所以现在 Artist 有一个
List
并且 Album 有List
我必须稍微修改生成的 xml 以确保它正确地填充了类。这是代码
希望有帮助。这不包括您想要多个艺术家的情况。
I modified your classes a little bit so now Artist has a
List<Album>
and Album hasList<Songs>
I had to modify the generated xml a little bit to make sure it populates the classes properly. Here is the code
Hope that helps. This doesn't cover the case where you want more than one artist.