DataContractSerializer类的序列化反序列化规则

1:不使用特性(DataContract)的前提下

         规则一:对象的公有字段和属性都会参与序列化成xml文件;

         规则二:对象所在的名称空间会作为xml文件的命名空间;

         规则三:xml文件的节点顺序按照英文字母排序(和对象的公有字段和属性顺序无关);

         规则四:如果对象存在继承关系,那么xml文件节点顺序是优先取基类的成员;

         规则五:xml文件反序列化成对象时按照xml文件节点的英文字母排序

         规则六:如果对象存在继承关系,那么优先反序列化基类;

上代码:

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.IO;
  6 using System.Runtime.Serialization;
  7 
  8 namespace ConsoleApplication4
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             t_domain ddd = new t_domain();
 15             ddd.domainid = 1;
 16             ddd.title = "eee";
 17             ddd.creationby=2;
 18             ddd.modifieddate=DateTime.Now;
 19 
 20 
 21             string x = Serialize<t_domain>(ddd);
 22             x = "<t_domain xmlns=\"http://schemas.datacontract.org/2004/07/ConsoleApplication4\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><creationby>2</creationby><domainid>1</domainid><modifieddate>2012-07-05T11:02:31.2424918+08:00</modifieddate><title>eee</title></t_domain>";
 23             string y = "<t_domain xmlns=\"http://schemas.datacontract.org/2004/07/ConsoleApplication4\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><modifieddate>2012-07-04T17:23:57.3420298+08:00</modifieddate><title>eee</title><creationby>1</creationby><domainid>1</domainid></t_domain>";
 24 
 25 
 26             t_domain xxx = Deserialize<t_domain>(x);
 27             t_domain yyy = Deserialize<t_domain>(y);
 28            
 29         }
 30 
 31         public static string Serialize<T>(dynamic value)
 32         {
 33             MemoryStream msTemp = null;
 34 
 35             try
 36             {
 37                 msTemp = new MemoryStream();
 38 
 39                 DataContractSerializer dcsSerializer = new DataContractSerializer(typeof(T));
 40 
 41                 dcsSerializer.WriteObject(msTemp, value);
 42 
 43                 byte[] array = msTemp.ToArray();
 44 
 45                 string strValue = Encoding.UTF8.GetString(array, 0, array.Length);
 46 
 47                 return strValue;
 48             }
 49             catch
 50             {
 51                 throw;
 52             }
 53             finally
 54             {
 55                 msTemp.Close();
 56                 msTemp.Dispose();
 57             }
 58         }
 59 
 60         public static T Deserialize<T>(string value)
 61         {
 62             MemoryStream msTemp = null;
 63 
 64             try
 65             {
 66                 DataContractSerializer dcsSerializer = new DataContractSerializer(typeof(T));
 67 
 68                 msTemp = new MemoryStream(Encoding.UTF8.GetBytes(value));
 69 
 70                 return (T)dcsSerializer.ReadObject(msTemp);
 71             }
 72             catch
 73             {
 74                 throw;
 75             }
 76             finally
 77             {
 78                 msTemp.Close();
 79                 msTemp.Dispose();
 80             }
 81         }
 82     }
 83 
 84     public class t_domain
 85     {
 86         public virtual Nullable<System.DateTime> modifieddate
 87         {
 88             get;
 89             set;
 90         }
 91 
 92         public virtual string title
 93         {
 94             get;
 95             set;
 96         }
 97         public virtual long domainid
 98         {
 99             get;
100             set;
101         }
102         public virtual Nullable<long> creationby
103         {
104             get;
105             set;
106         }
107     }
108     public class baseDomain
109     {
110         //public Nullable<long> creationby = 1;
111 
112 
113         //public Nullable<System.DateTime> modifieddate = DateTime.Now;
114 
115         private Nullable<System.DateTime> m_modifieddate=System.DateTime.Now;
116         public virtual Nullable<System.DateTime> modifieddate
117         {
118             get
119             {
120                 return m_modifieddate;
121             }
122             set
123             {
124                 m_modifieddate = value;
125             }
126         }
127 
128 
129         private Nullable<long> m_creationby = 1;
130         public virtual Nullable<long> creationby
131         {
132             get
133             {
134                 return m_creationby;
135             }
136             set
137             {
138                 m_creationby = value;
139             }
140         }
141 
142     }
143 
144 }

PS:注意第23行代码中的xml文件刻意打乱了节点顺序,那么反序列化后,对象的creationby和domainid属性将得不到值,因为反序列化器认为到title节点(按字母排序)的时候已经结束。

2:使用特性(DataContract)的前提下

原文地址:https://www.cnblogs.com/syf/p/2577537.html