您所在的位置: 首页 > 开发 > 语言&工具 >

我的WCF之旅(4):WCF中的序列化(2)(2)

http://developer.51cto.com  2008-03-13 13:44  Artech  博客园  我要评论(0)
  • 摘要:提到XMLSerializer,我想绝大多数人都知道这是asmx采用的Serializer。首先我们还是来看一个例子,通过比较Managed Type的结构和生成的XML的结构来总结这种序列化方式采用的是怎样的一种Mapping方式。
  • 标签:WCF  序列化  Serializer  XML

编写Serialization的Code

static void SerializeViaXMLSerializer()
{
XMLProduct product = new XMLProduct(Guid.NewGuid(), "Dell PC", "Xiamen FuJian", 4500);
XMLOrder order = new XMLOrder(Guid.NewGuid(), DateTime.Today, product, 300);
string fileName = _basePath + "Order.XmlSerializer.xml";
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(XMLOrder));
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs))
{
serializer.Serialize(writer, order);
}
}
Process.Start(fileName);
}

调用上面定义的方法,生成序列化的XML。

http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
b695fd18-9cd7-4792-968a-0c0c3a3962c2
2007-03-09T00:00:00+08:00

23a2fe03-d0a0-4ce5-b213-c7e5196af566
Dell PC
4500

300

这里我们总结出以下的Mapping关系:

1、Root Element被指定为类名。
2、不会再Root Element中添加相应的Namaspace。
3、对象成员以XML Element的形式输出。
4、对象成员出现的顺利和在Type定义的顺序一致。
5、只有Public Field和可读可写得Proppery才会被序列化到XML中——比如定义在XMLProduct中的internal string ProducingArea没有出现在XML中。
6、Type定义的时候不需要运用任何Attribute。

以上这些都是默认的Mapping关系,同DataContractSerializer一样,我们可以通过在Type以及它的成员中运用一些Attribute来改这种默认的Mapping。

Root Element名称之后能为类名。

1、可以在Type上运用XMLRoot,通过Namaspace参数在Root Element指定Namespace。
2、可以通过在类成员上运用XMLElement Attribute和XMLAttribute Attribute指定对象成员转化成XMLElement还是XMLAttribute。并且可以通过NameSpace参数定义Namespace。
3、可以在XMLElement或者XMLAttribute Attribute 通过Order参数指定成员在XML出现的位置。
4、可以通过XmlIgnore attribute阻止对象成员被序列化。

基于上面这些,我们重新定义了XMLProduct和XMLOrder。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Artech.WCFSerialization
{
public class XMLProduct
{
Private Fields#region Private Fields
private Guid _productID;
private string _productName;
private string _producingArea;
private double _unitPrice;
#endregion

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
[XmlAttribute("id")]
public Guid ProductID
{
get { return _productID; }
set { _productID = value; }
}

[XmlElement("name")]
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
[XmlElement("producingArea")]
public string ProducingArea
{
get { return _producingArea; }
set { _producingArea = value; }
}

[XmlElement("price")]
public double UnitPrice
{
get { return _unitPrice; }
set { _unitPrice = value; }
}

#endregion

}
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Artech.WCFSerialization
{
[XmlRoot(Namespace = "http://artech.wcfSerialization/Samples/Order")]
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
[XmlAttribute("id")]
public Guid OrderID
{
get { return _orderID; }
set { _orderID = value; }
}
[XmlElement(ElementName = "date",Order = 3)]
public DateTime OrderDate
{
get { return _orderDate; }
set { _orderDate = value; }
}
[XmlElement(ElementName = "product", Order = 1, Namespace = "Http://Artech.

WCFSerialization/Samples/Product")]
public XMLProduct Product
{
get { return _product; }
set { _product = value; }
}

[XmlElement(ElementName = "quantity", Order = 2)]
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);
}
}
}


共3页: 上一页 [1] 2 [3] 下一页
【内容导航】
WCF开发基础
文档格式标准开战 OOXML成国际标准
XML可扩展标识语言详解
JSP开发基础教程
PHP开发基础入门
 
 验证码: (点击刷新验证码)   匿名发表
  • C#图解教程

  • 作者:苏林,朱晔
  • 本书是一本广受赞誉的C#教程。它以图文并茂的形式,用朴实简洁的文字,并辅之以大量表格和代码示例,精炼而全面地阐述了最新版C..
Copyright©2005-2008 51CTO.COM 版权所有