首页 > 技术文章 > DatacontractAttribute的使用规则

chinaagan 2014-03-09 21:59 原文

关于DatacontractAttribute的使用规则和说明,

DatacontractAttribute是序列化类的另一种方法,和XmlMemberAttribute(也就是XmlElementAttribute)使用比较像。它的命名空间是System.Runtime.Serialization。

不同点:

DatacontractAttribute,必须定义DataMember,否则不序列化该字段;XmlMemberAttribute,默认是XmlElement。

序列化方法不一样,DatacontractAttribute使用DataContractSerializer;另一个使用XmlSerializer。

代码如下,

定义DataContract类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Data;
using System.ServiceModel;
using System.Xml;
using System.Runtime.Serialization;

namespace Artech.XmlSerializerDemos
{
    [DataContract]
    public class DataBase2 
    {
        [DataMember(Order=1)]
        public Guid ID
        {
            get;
            set;
        }

        private DateTime _date;
        [DataMember(Order=2)]
        public DateTime Date
        {
            get;
            set;
        }

       [DataMember(Order = 3)]
        public string Customer
        {
            get;
            set;
        }

        [DataMember(Order = 4)]
        public string ShipAddress
        {
            get;
            set;
        }

        [DataMember(Order = 5)]
        public double TotalPrice
        {
            get;
            set;
        }
    }

    public class Order2 : DataBase2
    {
        [DataMember(Order = 1)]
        public string PaymentType
        {
            get;
            set;
        }
    }
}
View Code

调用DataContract

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace Artech.XmlSerializerDemos
{
    class Program
    {
        static void Main(string[] args)
        {
            #region DataContract
            Order2 order = new Order2()
            {
                ID = Guid.NewGuid(),
                Date = DateTime.Today,
                Customer = "Foo",
                ShipAddress = "airport address",
                TotalPrice = 8888,
                PaymentType = "credit card"

            };
            Serialize<Order2>(order, @"E:\Order.xml");
            #endregion

            #region xml Attribute
            //Order order = new Order()
            //{
            //    ID = Guid.NewGuid(),
            //    Date = DateTime.Today,
            //    Customer = "Foo",
            //    ShipAddress = "airport address"

            //};
            //Serialize<Order>(order, @"E:\Order.xml");
            #endregion
           
        }

        static void Serialize<T>(T instance, string fileName)
        {
            #region xml Attribute
            //using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
            //{
            //    XmlSerializer serializer = new XmlSerializer(typeof(T));
            //    serializer.Serialize(writer, instance);
            //}
            #endregion

            #region  xml datacontract
            using (XmlWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(writer, instance);
            }
            #endregion
            Process.Start(fileName);
        }
        
    }
}
View Code

 

推荐阅读