日期:2014-05-18 浏览次数:20992 次
class Program { static void Main(string[] args) { //var c = new Contract { // Title = "合同版权", // Copyrights = new List<Copyright> { // new Copyright{WorksName="worksname1"}, // new Copyright{WorksName="worksname2"}, // new Copyright{WorksName="worksname3"} // } //}; //using (MemoryStream ms = new MemoryStream()) { // var xs = new XmlSerializer(typeof(Contract)); // xs.Serialize(ms, c); // Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer())); //} string data = "<Contract><Title>合同版权</Title><Copyrights><Copyright ><WorksName>青色平原</WorksName></Copyright><Copyright><WorksName>西游记</WorksName></Copyright></Copyrights></Contract>"; var xs = new XmlSerializer(typeof(Contract)); var c = xs.Deserialize(new StringReader(data)) as Contract; Console.WriteLine(c.Copyrights.Count); foreach(var each in c.Copyrights){ Console.WriteLine(each.WorksName); } Console.WriteLine("press any key to exit."); Console.ReadLine(); } } [DataContract] public class Contract { [DataMember] public string Title { get; set; } private List<Copyright> m_copyrights = new List<Copyright>(); [DataMember] public List<Copyright> Copyrights { get { return this.m_copyrights; } set { this.m_copyrights = value; } } } //版权类 [DataContract] public class Copyright { [DataMember] public string WorksName { get; set; } }
------解决方案--------------------
1. 指定次序(默认C在T前面,而Copyrights在Title后面):
[DataMember(Order=1