我无法将xml反序列化为包含列表的类。一切都在对象中构建良好,直到它到达列表。我已经阅读了这里的所有帖子,并尝试了几乎所有的内容,但它似乎仍然没有结束。
XML:(“text.xml”)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example>
<id>123456</id>
<messages>
<message>
<words>Hello</words>
</message>
<message>
<words>world</words>
</message>
</messages>
</example>
代码:
static void Main(string[] args) {
try {
XmlSerializer serializer = new XmlSerializer(typeof(Example));
XmlDocument objDoc = new XmlDocument();
objDoc.Load("test.xml");
XmlReader reader = XmlReader.Create(new StringReader(objDoc.OuterXml));
Example example;
example = (Example)serializer.Deserialize(reader);
Console.WriteLine(example.Id.ToString());
Console.WriteLine(example.Messages.Count.ToString());
Console.WriteLine(example.Messages[0].Words);
}
catch(Exception ex) {
Console.Write(ex.Message);
}
Console.ReadLine();
}
[XmlRoot("example")]
public class Example {
[XmlElement("id")]
public int Id {
get {
return id;
}
set {
id = value;
}
}
private int id;
[XmlElement("messages")]
public List<Message> Messages {
get {
return messages;
}
set {
messages = value;
}
}
private List<Message> messages;
}
[XmlRoot("message")]
public class Message {
[XmlElement("words")]
public string Words {
get {
return words;
}
set {
words = value;
}
}
private string words;
}
输出:
123456
1
我是否错过了Serializer需要的标签?或者我正在努力实现的目标是不可能的?非常感谢任何指针或向正确方向推..
答案 0 :(得分:2)
试试这个:
注意XmlArray和XmlArraItem的定义。我相信其他人会有更好的方法,但这会使它运转起来。此外,您应该使用&#39;使用&#39;处理xml阅读器的声明。
public class Program
{
public static void Main(string[] args)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Example));
using (XmlReader reader = XmlReader.Create("test.xml"))
{
Example example = (Example)serializer.Deserialize(reader);
Console.WriteLine(example.Id.ToString());
Console.WriteLine(example.Messages.Count.ToString());
Console.WriteLine(example.Messages[0].Words);
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.ReadLine();
}
[XmlRoot("example")]
public class Example
{
[XmlElement("id")]
public int Id
{
get
{
return id;
}
set
{
id = value;
}
}
private int id;
[XmlArray("messages")]
[XmlArrayItem("message", typeof(Message))]
public List<Message> Messages
{
get
{
return messages;
}
set
{
messages = value;
}
}
private List<Message> messages;
}
[XmlRoot("message")]
public class Message
{
[XmlElement("words")]
public string Words { get; set; }
}
答案 1 :(得分:1)
我明白为什么你会感到困惑。但是,您的对象结构与您的XML不匹配。您有一个messages
节点,该节点包含该列表。所以结构看起来很像:
[XmlRoot("example")]
public class Example
{
[XmlElement("id")]
public int Id { get; set; }
[XmlElement("messages")]
public Messages Messages { get; set; }
}
public class Messages
{
[XmlElement("message")]
public List<Message> Message { get; set; }
}
public class Message
{
[XmlElement("words")]
public string Words { get; set; }
}
针对该结构运行代码可以获得正确的输出。
答案 2 :(得分:1)
试试这个
[XmlRoot("example")]
public class Example {
[XmlElement("id")]
public int Id { get; set;}
[XmlElement("messages")]
public Message messages {get; set;}
}
[XmlRoot("messages")]
public class Messages {
[XmlElement("message")]
public List<Message> message { get; set;}
}
[XmlRoot("message")]
public class Message {
[XmlElement("words")]
public string Words { get; set;}
}