一、本章主要介绍.NET xml序列化,构建XML序列化的框架类,主要放在你的框架项目里面,类名字命名为Serialiser,我的框架项目名称是MingXu.FrameWork,我将Serialiser放到它下面,MingXu.FrameWork类型为Class Library
二、序列化类用到了2个dll,分别为:
System.Xml.Serialization:XML序列化类
System.IO:IO类
注意添加对System.Xml.Serialization及System.IO的引用。
如果不知道怎么添加对dll的引用,请参见微软 MSMQ 使用 第二章 构造MSMQ的框架类
三、代码如下:代码都有注释,所以不做过多的解释了,如果有看不明白的,请在评论里面留言,我在加注释
//******************************************************
//文件名:Serialiser.cs
//功能:Xml 序列化类
//作者:徐明
//日期:2009-11-4
//*******************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
namespace MingXu.FrameWork
{
/// <summary>
/// Generic XML serialiser
/// </summary>
/// <typeparam name="T">序列化类型</typeparam>
public sealed class Serialiser<T>
{
/// <summary>
/// 功能:序列化
/// 作者:Jerry
/// 日期:2009-11-4
/// </summary>
/// <param name="obj">序列化对象</param>
/// <param name="filepath">路径</param>
public static void Serialize(T obj, string filepath)
{
if (string.IsNullOrEmpty(filepath) || obj==null)
return;
StreamWriter myWriter = null;
try
{
XmlSerializer mySerializer = new XmlSerializer(obj.GetType());
myWriter = new StreamWriter(filepath);
mySerializer.Serialize(myWriter, obj);
}
catch (Exception ex)
{
//Log.Debug(ex.Message);
}
finally
{
if (myWriter != null)
{
myWriter.Close();
myWriter.Dispose();
myWriter = null;
}
}
}
/// <summary>
/// 功能:反序列化
/// 作者:Jerry
/// 日期:2009-11-4
/// </summary>
/// <param name="filepath">路径</param>
/// <returns>反序列化的对象</returns>
public static T Deserialize(string filepath)
{
T myObj = default(T);
if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath))
return myObj;
FileStream myFileStream = null;
try
{
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
myFileStream = new FileStream(filepath, FileMode.Open);
myObj = (T)mySerializer.Deserialize(myFileStream);
}
catch (Exception ex)
{
//Log.Debug(ex.Message);
}
finally
{
if (myFileStream != null)
{
myFileStream.Close();
myFileStream.Dispose();
myFileStream = null;
}
}
return myObj;
}
}
}
三、序列化类的使用
1、首先新建一个控制台项目MingXu.ConsoleApp,注意添加对框架类的引用,然后构造一个测试类,用来进行序列化操作,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace MingXu.ConsoleApp
{
public class xmlSerializationTest
{
string _availableVersion;
[XmlElement("availableVersion")]
public string AvailableVersion
{
get { return _availableVersion; }
set { _availableVersion = value; }
}
}
}
2、然后在Main函数里添加代码进行操作:
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mingxu.TestClass;
using MingXu.FrameWork;
namespace MingXu.ConsoleApp
{
class PRogram
{
static void Main(string[] args)
{
ser();
deser();
}
/// <summary>
/// 序列化
/// </summary>
private static void ser()
{
xmlSerializationTest test = new xmlSerializationTest();
test.AvailableVersion = "Hello world!";
string path = "d:\\xmlSerializationTest.xml";
Serialiser<xmlSerializationTest>.Serialize(test, path);
}
/// <summary>
/// 反序列化
/// </summary>
private static void deser()
{
string path = "d:\\xmlSerializationTest.xml";
xmlSerializationTest test = Serialiser<xmlSerializationTest>.Deserialize(path);
Console.WriteLine(test.AvailableVersion);
string bb = Console.ReadLine();
}
}
资料引用:http://www.knowsky.com/539456.html