我想序列化和反序列化这样的对象:
public class Root
{
public string title;
[XmlArray("lista")]
[XmlArrayItem("Parameter")]
public List<Parameter> lista;
}
public class Parameter : IXmlSerializable
{
public string name;
public int val;
public void WriteXml(XmlWriter writer)
{
writer.WriteElementString("name",name);
writer.WriteElementString("val",val.ToString());
}
public void ReadXml(XmlReader reader)
{
reader.ReadToDescendant("name");
name = reader.ReadElementContentAsString();
val = reader.ReadElementContentAsInt();
}
public XmlSchema GetSchema()
{
return (null);
}
}
问题是当我反序列化这个对象时,它只添加了列表的第一个元素。例子:
序列化:
<Root>
<title>title</title>
<lista>
<Parameter>
<name>pa</name>
<val>1</val>
</Parameter>
<Parameter>
<name>pb</name>
<val>2</val>
</Parameter>
</lista>
</Root>
反序列化:
<Root>
<title>title</title>
<lista>
<Parameter>
<name>pa</name>
<val>1</val>
</Parameter>
</lista>
</Root>
我已经实现了一个Fiddle示例。有人可以帮助我吗?