... ...续Part I([原创] 我的WCF之旅(4):WCF中的序列化(Serialization)- Part I)
XMLSerializer
提到XMLSerializer,我想绝大多数人都知道这是asmx采用的Serializer。首先我们还是来看一个例子,通过比较Managed Type的结构和生成的XML的结构来总结这种序列化方式采用的是怎样的一种Mapping方式。和DataContractSerialzer Sample一样,我们要定义用于序列化对象所属的Type——XMLOrder和XMLProduct,他们和相面对应的DataContractOrder和DataContractProduct具有相同的成员。
using
System;
using
System.Collections.Generic;
using
System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Artech.WCFSerialization
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class XMLProduct
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Fields#region Private Fields
private Guid _productID;
private string _productName;
private string _producingArea;
private double _unitPrice;
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
public XMLProduct()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("The constructor of XMLProduct has been invocated!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XMLProduct(Guid id, string name, string producingArea, double price)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._productID = id;
this._productName = name;
this._producingArea = producingArea;
this._unitPrice = price;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
public Guid ProductID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productID; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productID = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string ProductName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productName; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productName = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
internal string ProducingArea
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _producingArea; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _producingArea = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public double UnitPrice
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _unitPrice; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _unitPrice = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
using
System;
using
System.Collections.Generic;
using
System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Artech.WCFSerialization
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class XMLOrder
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Guid _orderID;
private DateTime _orderDate;
private XMLProduct _product;
private int _quantity;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
public XMLOrder()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._orderID = new Guid();
this._orderDate = DateTime.MinValue;
this._quantity = int.MinValue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("The constructor of XMLOrder has been invocated!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XMLOrder(Guid id, DateTime date, XMLProduct product, int quantity)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._orderID = id;
this._orderDate = date;
this._product = product;
this._quantity = quantity;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
public Guid OrderID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderID; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderID = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DateTime OrderDate
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderDate; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderDate = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XMLProduct Product
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _product; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _product = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Quantity
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _quantity; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _quantity = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format("ID: {0}\nDate:{1}\nProduct:\n\tID:{2}\n\tName:{3}\n\tProducing Area:{4}\n\tPrice:{5}\nQuantity:{6}",
this._orderID,this._orderDate,this._product.ProductID,this._product.ProductName,this._product.ProducingArea,this._product.UnitPrice,this._quantity);
}
}
}
编写Serialization的Code.
static void SerializeViaXMLSerializer()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XMLProduct product = new XMLProduct(Guid.NewGuid(), "Dell PC", "Xiamen FuJian", 4500);
XMLOrder order = new XMLOrder(Guid.NewGuid(), DateTime.Today, product, 300);
string fileName = _basePath + "Order.XmlSerializer.xml";
using (FileStream fs = new FileStream(fileName, FileMode.Create))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlSerializer serializer = new XmlSerializer(typeof(XMLOrder));
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
serializer.Serialize(writer, order);
}
}
Process.Start(fileName);
}
调用上面定义的方法,生成序列化的XML。
<?
xml version="1.0" encoding="utf-8"
?>
<
XMLOrder
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
>
<
OrderID
>
b695fd18-9cd7-4792-968a-0c0c3a3962c2
</
OrderID
>
<
OrderDate
>
2007-03-09T00:00:00+08:00
</
OrderDate
>
<
Product
>
<
ProductID
>
23a2fe03-d0a0-4ce5-b213-c7e5196af566
</
ProductID
>
<
ProductName
>
Dell PC
</
ProductName
>
<
UnitPrice
>
4500
</
UnitPrice
>
</
Product
>
<
Quantity
>
300
</
Quantity
>
</
XMLOrder
>
这里我们总结出以下的Mapping关系:
-
Root Element被指定为类名。
-
不会再Root Element中添加相应的Namaspace。
-
对象成员以XML Element的形式输出。
-
对象成员出现的顺利和在Type定义的顺序一致。
-
只有Public Field和可读可写得Proppery才会被序列化到XML中——比如定义在XMLProduct中的internal string ProducingArea没有出现在XML中。
-
Type定义的时候不需要运用任何Attribute。
以上这些都是默认的Mapping关系,同DataContractSerializer一样,我们可以通过在Type以及它的成员中运用一些Attribute来改这种默认的Mapping。
-
Root Element名称之后能为类名。
-
可以在Type上运用XMLRoot,通过Namaspace参数在Root Element指定Namespace。
-
可以通过在类成员上运用XMLElement Attribute和XMLAttribute Attribute指定对象成员转化成XMLElement还是XMLAttribute。并且可以通过NameSpace参数定义Namespace。
-
可以在XMLElement或者XMLAttribute Attribute 通过Order参数指定成员在XML出现的位置。
-
可以通过XmlIgnore attribute阻止对象成员被序列化。
基于上面这些,我们重新定义了XMLProduct和XMLOrder。
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Xml.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Artech.WCFSerialization
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public class XMLProduct
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Fields#region Private Fields
private Guid _productID;
private string _productName;
private string _producingArea;
private double _unitPrice;
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
public XMLProduct()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("The constructor of XMLProduct has been invocated!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XMLProduct(Guid id, string name, string producingArea, double price)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._productID = id;
this._productName = name;
this._producingArea = producingArea;
this._unitPrice = price;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
[XmlAttribute("id")]
public Guid ProductID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productID; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productID = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement("name")]
public string ProductName
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productName; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productName = value; }
}
[XmlElement("producingArea")]
public string ProducingArea
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _producingArea; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _producingArea = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement("price")]
public double UnitPrice
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _unitPrice; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _unitPrice = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Xml.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Artech.WCFSerialization
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
[XmlRoot(Namespace = "http://artech.wcfSerialization/Samples/Order")]
public class XMLOrder
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Guid _orderID;
private DateTime _orderDate;
private XMLProduct _product;
private int _quantity;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constructors#region Constructors
public XMLOrder()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._orderID = new Guid();
this._orderDate = DateTime.MinValue;
this._quantity = int.MinValue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("The constructor of XMLOrder has been invocated!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public XMLOrder(Guid id, DateTime date, XMLProduct product, int quantity)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._orderID = id;
this._orderDate = date;
this._product = product;
this._quantity = quantity;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Properties#region Properties
[XmlAttribute("id")]
public Guid OrderID
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderID; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderID = value; }
}
[XmlElement(ElementName = "date",Order = 3)]
public DateTime OrderDate
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderDate; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderDate = value; }
}
[XmlElement(ElementName = "product", Order = 1, Namespace = "Http://Artech.WCFSerialization/Samples/Product")]
public XMLProduct Product
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _product; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _product = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement(ElementName = "quantity", Order = 2)]
public int Quantity
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _quantity; }
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _quantity = value; }
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format("ID: {0}\nDate:{1}\nProduct:\n\tID:{2}\n\tName:{3}\n\tProducing Area:{4}\n\tPrice:{5}\nQuantity:{6}",
this._orderID,this._orderDate,this._product.ProductID,this._product.ProductName,this._product.ProducingArea,this._product.UnitPrice,this._quantity);
}
}
}
重新进行一次Serialization。我们可以得到下面的XML。
<?
xml version="1.0" encoding="utf-8"
?>
<
XMLOrder
id
="9a0bbda4-1743-4398-bc4f-ee216e02695b"
xmlns
="http://artech.wcfSerialization/Samples/Order"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
>
<
product
id
="4e3aabe5-3a51-4000-9fd8-d821d164572a"
xmlns
="Http://Artech.WCFSerialization/Samples/Product"
>
<
name
>
Dell PC
</
name
>
<
producingArea
>
Xiamen FuJian
</
producingArea
>
<
price
>
4500
</
price
>
</
product
>
<
quantity
>
300
</
quantity
>
<
date
>
2007-03-09T00:00:00+08:00
</
date
>
</
XMLOrder
>
分析完XMLSerializer的Serialization功能,我们照例来分析它的反向过程—Deserialization。下面的Deserialization的Code。
static void DeserializeViaXMLSerializer()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string fileName = _basePath + "Order.XmlSerializer.xml";
XMLOrder order;
using (FileStream fs = new FileStream(fileName, FileMode.Open))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XmlSerializer serializer = new XmlSerializer(typeof(XMLOrder), "http://artech.WCFSerialization/Samples");
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
order= serializer.Deserialize(reader) as XMLOrder;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(order);
Console.Read();
}
调用DeserializeViaXMLSerializer,得到下面的Screen Shot。下面显示的Order对象的信息和我们利用DataContractSerializaer进行Deserialization是的输出没有什么两样。不过有趣的是上面多出了两行额外的输出:The constructor of XMLProduct has been invocated! The constructor of XMLOrder has been invocated。而这个操作实际上是定义在XMLProduct和XMLOrder的默认(无参)构造函数里的。所此我们可以得出这样的结论——用XMLSerializer进程Deserialization,会调用的默认(无参)构造函数来初始化对象。
![](https://i-blog.csdnimg.cn/blog_migrate/11d982eb67fb0bfddeb82b822236d17c.jpeg)
DataContractSerializer V.S. XMLSerializer
上面我们分别分析了两种不同的Serializer,现在我们来简单总结一下他们的区别:
特性 | XMLSerializer | DataContractSerializer |
默认Mapping | 所有Public Field和可读可写Property | 所有DataMember Filed、Property |
是否需要Attribute | 不需要 | DataContract DataMember或者Serializable |
成员的默认次序 | Type中定义的顺序 | 字母排序 |
兼容性 | .asmx | Remoting |
Deserialzation | 调用默认构造函数 | 不会调用 |
WCF相关内容:
[原创]我的WCF之旅(1):创建一个简单的WCF程序
[原创]我的WCF之旅(2):Endpoint Overview
[原创]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication)
[原创]我的WCF之旅(4):WCF中的序列化(Serialization)- Part I
[原创]我的WCF之旅(4):WCF中的序列化(Serialization)- Part II
[原创]我的WCF之旅(5):Service Contract中的重载(Overloading)
[原创]我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案
[原创]我的WCF之旅(9):如何在WCF中使用tcpTrace来进行Soap Trace
[原创]我的WCF之旅(10): 如何在WCF进行Exception Handling
[原创]我的WCF之旅(11):再谈WCF的双向通讯-基于Http的双向通讯 V.S. 基于TCP的双向通讯
[原创]我的WCF之旅(12):使用MSMQ进行Reliable Messaging
[原创]我的WCF之旅(13):创建基于MSMQ的Responsive Service