序列化(serialization) & 反序列化(de-serialization)- 序列化到内存xml
2008-08-08 17:46
495 查看
序列化:提供了可以把内存中的对象的snapshot保存到xml,二进制文件,内存的机制;
在需要的时候,可以用反序列化就可以得到当时的 snapshot
----------------------------------------------------------
最近在做项目的时候,需要把一个List 序列化到内存中,但是以XML格式。
经过一定的research and try 终于解决问题啦:
假设这里有一个list需要序列化:
List<Person> List = GetList();
XmlSerializer xs = new XmlSerializer(typeof(List<Person>)
, new Type[] { typeof(PersonAttribute)});
//atra types input here
MemoryStream ms = new MemoryStream();
//序列化到内存流,这个时候是字节
xs.Serialize(ms, FilterEntryList);
byte[] content = new byte[ms.Length];
ms.Position = 0;//this is important, you should reset the pointer
ms.Read(content, 0, (int)ms.Length);
//(int)ms.Length, here you can do further processing in case the length too large to miss some bytes data
//change the encoding
ASCIIEncoding ae = new ASCIIEncoding();
string XmlContent = ae.GetString(content);
//load the string to xmldocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(XmlContent);
//then you have get a xmldocument in memory
wisdom
在需要的时候,可以用反序列化就可以得到当时的 snapshot
----------------------------------------------------------
最近在做项目的时候,需要把一个List 序列化到内存中,但是以XML格式。
经过一定的research and try 终于解决问题啦:
假设这里有一个list需要序列化:
List<Person> List = GetList();
XmlSerializer xs = new XmlSerializer(typeof(List<Person>)
, new Type[] { typeof(PersonAttribute)});
//atra types input here
MemoryStream ms = new MemoryStream();
//序列化到内存流,这个时候是字节
xs.Serialize(ms, FilterEntryList);
byte[] content = new byte[ms.Length];
ms.Position = 0;//this is important, you should reset the pointer
ms.Read(content, 0, (int)ms.Length);
//(int)ms.Length, here you can do further processing in case the length too large to miss some bytes data
//change the encoding
ASCIIEncoding ae = new ASCIIEncoding();
string XmlContent = ae.GetString(content);
//load the string to xmldocument
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(XmlContent);
//then you have get a xmldocument in memory
wisdom
相关文章推荐
- XML序列化和反序列化
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- 序列化,反序列化(xml)字符串
- c# xml序列化和反序列化
- XML序列化和反序列化及实例
- XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置
- XML序列化及反序列化
- Java对象的XML序列化和反序列化 - (3)
- Xml序列化和反序列化
- JAXB序列化和反序列化XML源码(可直接使用)
- Xml序列化和反序列化对象-使用MemoryStream-实践
- 用 XStream 序列化/反序列化 XML 为 Java 对象(实例)
- .NET对象的XML序列化和反序列化
- XML序列化和反序列化
- C#中xml序列化和反序列化
- XML序列化和反序列化
- XmlSerializer 对象的Xml序列化和反序列化
- XML序列化和反序列化
- [ServiceStack.Text] .net序列化,反序列化组件(号称最快,支持 JSON,XML,JSV格式)
- C# Json库 和 xml 序列化反序列化 存在的问题