我正在尝试使用以下代码将自定义对象序列化为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;
但是我在第二行得到了例外:
matchlist对象定义为:
public class MatchList
{
public Header Header { get; set; }
[XmlArray("Lists")]
[XmlArrayItem("List")]
public virtual List<List> Lists { get; set; }
}
答案 0 :(得分:2)
如果您的对象中有一个不需要序列化的属性,请添加属性
[XmlIgnore]
在它之上。要找到有问题的属性,请单击异常消息中的“查看详细信息...”,然后一直单击“内部异常”。
答案 1 :(得分:0)
[XmlArray("Lists")]
[XmlArrayItem("List")]
public virtual List<object> Lists { get; set; }
并添加标记knownTypeAttribute,其可序列化类型为object