随着面向服务(SOA)的开发方式的兴起,客户端和服务端之间的消息传送,很多采用了XML的格式。但是大家在日常的开发中,应该会有这么种体验,就是组织xml格式的代码太繁琐,这篇随笔也是为了和大家分享下简便的组织xml字符串的解决方案。
闲话不多说,我们直接上源码:
(1)自定义的实体类源码(简单的序列化我就不在赘述),大家可以仔细看下这个实体类源码中包含了好几个类,然后类之间有着层级调用,这样的方式就是为了在序列化的时候实现xml元素包含元素的形式;如果想要实现同一个元素并列展示,那么就需要声明为数组类型 如下的public long[] BlockID { get; set; }
所有要序列化的字段或者类都需要为public
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml.Serialization; 6 7 namespace Message.Entity 8 { 9 [XmlRoot("Message")]//标记根节点的名字 10 public class DYAreaTypeMessage 11 { 12 public Header Header{ get; set; } 13 public Payload Payload { get; set; } 14 } 15 16 [Serializable] 17 public class Header 18 { 19 public string Verb { get; set; } 20 public string Noun { get; set; } 21 } 22 23 [Serializable] 24 public class Payload 25 { 26 public NetworkChangeDetail NetworkChangeDetail { get; set; } 27 } 28 29 [Serializable] 30 public class DYAreaType 31 { 32 [XmlElement("BlockID",IsNullable=false)]//如果自己不定义元素的名字 ,那么序列化生成的元素就会变成字段类型的名称 33 public long[] BlockID { get; set; }//注意(1)该地方是一个数组的形式,不可以用list代替,不然将不能序列化 34 } 35 36 [Serializable] 37 public class NetworkChangeDetail 38 { 39 public DYAreaType DYAreaType 40 { 41 get; 42 set; 43 } 44 45 [XmlElement("MessageID",IsNullable=false)] 46 public string MessageID 47 { 48 get; 49 set; 50 } 51 [XmlElement("Buro")] 52 public string Buro 53 { 54 get; 55 set; 56 } 57 [XmlElement("SubBuro")] 58 public string SubBuro 59 { 60 get; 61 set; 62 } 63 64 [XmlElement("Description")] 65 public string Description 66 { 67 get; 68 set; 69 } 70 [XmlElement("DateTime")] 71 public string DateTime 72 { 73 get; 74 set; 75 } 76 }
序列化的实现方法其实很简单 使用.net提供的XmlSerializer 类就可以了 ,
XmlSerializer serializer = new XmlSerializer(typeof(类名)); serializer.Serialize(textWriter,类的实例);//参数说明可查看msdn
但是我们一般会遇到一个一问题就是要指定xml的编码格式,这个地方让我也蛋疼了好一会,后来解决方式是这样的
MemoryStream ms = new MemoryStream();//定义一个内存流 XmlTextWriter textWriter = new XmlTextWriter(ms,Encoding.GetEncoding("gb2312"));//定义输出的编码格式
通过以上两句代码即可实现自定义编码格式,然后还有个就是把内存流转成字符串
string xmlMessage = Encoding.UTF8.GetString(ms.GetBuffer());
1 /// <summary> 2 /// 创建消息的xml语句 3 /// </summary> 4 /// <param name="blockID"></param> 5 /// <param name="buro"></param> 6 /// <param name="subBuro"></param> 7 /// <param name="Description"></param> 8 public string CreateMessageXML(long[] blockID, string buro, string subBuro, string Description, long activityID, out bool isSerializer) 9 { 10 isSerializer = false; 11 CISP.Message.Entity.DYAreaTypeMessage dyArea = new CISP.Message.Entity.DYAreaTypeMessage(); 12 dyArea.Header = new CISP.Message.Entity.Header() { Verb = "Update", Noun = "NetworkChangeDetail" }; 13 dyArea.Payload = new CISP.Message.Entity.Payload(); 14 dyArea.Payload.NetworkChangeDetail = new CISP.Message.Entity.NetworkChangeDetail() 15 { 16 MessageID = activityID.ToString(), 17 Buro = buro, 18 SubBuro = subBuro, 19 Description = Description, 20 DateTime = DateTime.Now.ToString("G") 21 }; 22 23 dyArea.Payload.NetworkChangeDetail.DYAreaType = new CISP.Message.Entity.DYAreaType() 24 { 25 BlockID = blockID 26 }; 27 28 MemoryStream ms = new MemoryStream(); 29 // XmlTextWriter textWriter = new XmlTextWriter(ms, Encoding.GetEncoding("UTF-8")); 30 StreamWriter textWriter = new StreamWriter(ms, Encoding.GetEncoding("gb2312")); 31 XmlSerializer serializer = new XmlSerializer(typeof(CISP.Message.Entity.DYAreaTypeMessage)); 32 serializer.Serialize(textWriter, dyArea); 33 34 string xmlMessage = Encoding.UTF8.GetString(ms.GetBuffer()); 35 isSerializer = true; 36 ms.Close(); 37 textWriter.Close(); 38 return xmlMessage; 39 }
(3)最后我们看下结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "gb2312" ?> //不知道为何指定为utf8变成了gb2312 <Message xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/2001/XMLSchema" > <Header> <Verb>Update</Verb> <Noun>NetworkChangeDetail</Noun> </Header> <Payload> <NetworkChangeDetail> <DYAreaType> <BlockID>30000299430897</BlockID> <BlockID>30000299511558</BlockID> <BlockID>30000101714620</BlockID> <BlockID>30000211294903</BlockID> <BlockID>30000299425996</BlockID> <BlockID>30000299425985</BlockID> </DYAreaType> <MessageID>604186</MessageID> <Buro>335100000</Buro> <SubBuro>0000145222</SubBuro> <Description>fff</Description> <DateTime>2013-12-9 13:34:52</DateTime> </NetworkChangeDetail> </Payload> </Message> |
http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html 这篇博客中提到以下注意点,仅供参考 没有去验证过。
我们在使用XmlSerializer序列化,初始化XmlSerializer对象时最好使用下面两个构造函数否则会引起内存泄漏。
XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, object)