当前位置:编程学习 > asp >>

我的WCF之旅(4):WCF中的序列化[下篇]

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} Date:{1} Product: ID:{2} Name:{3} Producing Area:{4} Price:{5} Quantity:{6}",
                this._orderID,this._orderDate,this._product.ProductID,this._product.ProductName,this._product.ProducingArea,this._product.UnitPrice,this._quantity);
        }
    }
}

编写Serialization的Code.

 

static void SerializeViaXMLSerializer()
        {
            XMLProduct produc

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,