2

我正在尝试使用以下代码将自定义对象序列化为 xml:

MatchList matchList = converter.Convert(excelStream);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MatchList));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, matchList);
string serializedXml = stringWriter.ToString();
txtResult.Text = serializedXml;

但是我在第二行得到了上面的异常:

匹配列表对象定义为:

public class MatchList
{
   public Header Header { get; set; }

   [XmlArray("Lists")]
   [XmlArrayItem("List")]
   public virtual List<List> Lists { get; set; }
}
4

2 回答 2

4

如果您的对象中有不需要序列化的属性,请添加该属性

[XmlIgnore]

在它的上面。要查找有问题的属性,请通过单击异常消息中的“查看详细信息...”进行挖掘,并一直单击“内部异常”。

于 2014-08-27T08:39:48.657 回答
1
[XmlArray("Lists")]
[XmlArrayItem("List")]
public virtual List<object> Lists { get; set; }

并将可序列化类型的标记 knownTypeAttribute 添加为object

于 2012-11-23T15:02:28.430 回答