使用以下类:
[Serializable]
public class OrderedClass
{
private int field1;
private string field2;
private string field3;
[XmlElement(Order = 30)]
public string Field3
{
get { return field3; }
set { field3 = value; }
}
public int Field1
{
get { return field1; }
set { field1 = value; }
}
[XmlElement(Order = 20)]
public string Field2
{
get { return field2; }
set { field2 = value; }
}
public OrderedClass()
{
field1 = 1;
field2 = "String1";
field3 = "String2";
}
}`
这会在不设置订单的情况下进行序列化public int Field1
吗?我认为这会在幕后将其顺序设置为 0,但是在尝试序列化时却遇到了反射错误
XmlSerializer orderedSerializer =
new XmlSerializer(typeof(OrderedClass));