关于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=)]
public Guid ID
{
get;
set;
} private DateTime _date;
[DataMember(Order=)]
public DateTime Date
{
get;
set;
} [DataMember(Order = )]
public string Customer
{
get;
set;
} [DataMember(Order = )]
public string ShipAddress
{
get;
set;
} [DataMember(Order = )]
public double TotalPrice
{
get;
set;
}
} public class Order2 : DataBase2
{
[DataMember(Order = )]
public string PaymentType
{
get;
set;
}
}
}

调用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 = ,
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);
} }
}

最新文章

  1. Maven创建web项目:SpringMVC+Mybatis 【转】
  2. ARM&amp;Linux 下驱动开发第二节
  3. centos6.5安装flume
  4. 【BZOJ 1090】[SCOI2003]字符串折叠
  5. 如何使用 Apache ab 以及 OneAPM 进行压力测试?
  6. alter和alert的一些问题
  7. Android之adb
  8. apache开源项目--Lens
  9. js 保留小数位数
  10. 利用PHPExcel转Excel柱形图
  11. 解開32位元Win 7記憶體4GB限制
  12. 8个新鲜的PHP常用代码
  13. iOS App内存优化之 解决UIImagePickerController的图片对象占用RAM过高问题
  14. MFC:位图和图标的设置
  15. HihoCoder - 1038 01背包 动态规划
  16. APK安装成功后点击&quot;打开&quot;,按Home键,在桌面点击图标后应用重启
  17. Linux实战
  18. 20165215 MySort的实现
  19. BZOJ3772精神污染——可持久化线段树+出栈入栈序
  20. pycharm 4注册码

热门文章

  1. java中的运算(2013-05-03-bd 写的日志迁移
  2. zookeeper的搭建方法
  3. 【转载】java 客户端链接不上redis解决方案 (jedis)
  4. 1,Python常用库之一:Numpy
  5. jQuery的Ajax初识
  6. Entity FrameWork和Dapper的使用
  7. JWT应用
  8. Jenkins拾遗--第五篇-git插件填坑
  9. 《Cracking the Coding Interview》——第17章:普通题——题目8
  10. 孤荷凌寒自学python第五天初识python的列表