XMLSerializer
提到XMLSerializer,我想绝大多数人都知道这是asmx采用的Serializer。首先我们还是来看一个例子,通过比较Managed Type的结构和生成的XML的结构来总结这种序列化方式采用的是怎样的一种Mapping方式。和DataContractSerialzer Sample一样,我们要定义用于序列化对象所属的Type——XMLOrder和XMLProduct,他们和相面对应的DataContractOrder和DataContractProduct具有相同的成员。
using System;
using System.Collections.Generic;
using System.Text;
namespace Artech.WCFSerialization
{
public class XMLProduct
{
Private Fields#region Private Fields
private Guid _productID;
private string _productName;
private string _producingArea;
private double _unitPrice;
Constructors#region Constructors
public XMLProduct()
{
Console.WriteLine("The constructor of XMLProduct has been invocated!");
}
public XMLProduct(Guid id, string name, string producingArea, double price)
{
this._productID = id;
this._productName = name;
this._producingArea = producingArea;
this._unitPrice = price;
}
#endregion
Properties#region Properties
public Guid ProductID
{
get { return _productID; }
set { _productID = value; }
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
internal string ProducingArea
{
get { return _producingArea; }
set { _producingArea = value; }
}
public double UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Artech.WCFSerialization
{
public class XMLOrder
{
private Guid _orderID;
private DateTime _orderDate;
private XMLProduct _product;
private int _quantity;
Constructors#region Constructors
public XMLOrder()
{
this._orderID = new Guid();
this._orderDate = DateTime.MinValue;
this._quantity = int.MinValue;
Console.WriteLine("The constructor of XMLOrder has been invocated!");
}
public XMLOrder(Guid id, DateTime date, XMLProduct product, int quantity)
{
this._orderID = id;
this._orderDate = date;
this._product = product;
this._quantity = quantity;
}
#endregion
Properties#region Properties
public Guid OrderID
{
get { return _orderID; }
set { _orderID = value; }
}
public DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}
public XMLProduct Product
{
get { return _product; }
set { _product = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
#endregion
public override string ToString()
{
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);
}
}
}