zoukankan      html  css  js  c++  java
  • DatacontractAttribute的使用规则

    关于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
  • 相关阅读:
    sharepoint的webpart开发
    触发器-插入不重复数据2
    触发器-插入不重复数据
    InfoPath本地发布及部署
    从30岁到35岁:为你的生命多积累一些厚度
    js中的forin
    js中的prototye
    无法绑定由多个部分组成的标示符
    Spring注入方式及用到的注解
    ( 转)mappingResource属性和mappingDirectoryLocations属性的使用
  • 原文地址:https://www.cnblogs.com/chinaagan/p/3590732.html
Copyright © 2011-2022 走看看