Microsoft在其文章“How to serialize an object to XML by using Visual C#。”
中提供了代码using System;
public class clsPerson
{
public string FirstName;
public string MI;
public string LastName;
}
class class1
{
static void Main(string[] args)
{
clsPerson p=new clsPerson();
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";
XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(Console.Out, p);
Console.WriteLine();
Console.ReadLine();
}
}
但是,为什么类clsPerson不需要用[DataContract]或[Serializable]属性标记?
答案 0 :(得分:4)
按照设计,XML序列化使用get AND set访问器序列化公共字段和公共属性。序列化类型还必须具有无参数构造函数。这是唯一的合同\ o /