本文描述如何使用CLR中的StringWriter,XmlSerializer将对象, 对象集合序列化为Xml格式的字符串, 同时描述如何进行反序列化.
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
C#版本: C#
3.0
开发环境: VS
2008
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
主要方法:
复制 保存using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Xml;
using
System.Xml.Serialization;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
namespace
ConsoleApplication2
{
public class SimpleSerializer
{
/// <summary>
/// 序列化对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">对象</param>
/// <returns></returns>
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
/// <summary>
/// 反序列化为对象
/// </summary>
/// <param name="type">对象类型</param>
/// <param name="s">对象序列化后的Xml字符串</param>
/// <returns></returns>
public static object Deserialize(Type type, string s)
{
using (StringReader sr = new StringReader(s))
{
XmlSerializer xz = new XmlSerializer(type);
return xz.Deserialize(sr);
}
}
}
}
定义一个用于测试的实体类: UserInfo
复制 保存using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Xml.Serialization;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
namespace
ConsoleApplication2
{
[Serializable]
public class UserInfo
{
private int m_UserId;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
[XmlElement("userId")]
public int UserId
{
get { return m_UserId; }
set { m_UserId = value; }
}
private string m_UserName;
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
[XmlElement("userName")]
public string UserName
{
get { return m_UserName; }
set { m_UserName = value; }
}
}
}
编写测试子程序:
复制 保存using System;
using
System.Collections.Generic;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
namespace
ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Program.TestOne();
Console.WriteLine(string.Empty);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Program.TestTwo();
Console.WriteLine(string.Empty);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Program.TestThree();
Console.WriteLine(string.Empty);
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
Program.TestFour();
}
public static void TestOne()
{
UserInfo info = new UserInfo() { UserId = 1, UserName = "Guozhijian" };
Console.WriteLine(SimpleSerializer.Serialize<UserInfo>(info));
}
public static void TestTwo()
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { UserId = 1, UserName = "Guozhijian" });
list.Add(new UserInfo() { UserId = 2, UserName = "Zhenglanzhen" });
Console.WriteLine(SimpleSerializer.Serialize<List<UserInfo>>(list));
}
public static void TestThree()
{
string s = SimpleSerializer.Serialize<UserInfo>(new UserInfo() { UserId = 1, UserName = "Guozhijian" });
UserInfo info = SimpleSerializer.Deserialize(typeof(UserInfo), s) as UserInfo;
Console.WriteLine(info.UserId.ToString() + ", " + info.UserName);
}
public static void TestFour()
{
List<UserInfo> list = new List<UserInfo>();
list.Add(new UserInfo() { UserId = 1, UserName = "Guozhijian" });
list.Add(new UserInfo() { UserId = 2, UserName = "Zhenglanzhen" });
string s = SimpleSerializer.Serialize<List<UserInfo>>(list);
List<UserInfo> newList = SimpleSerializer.Deserialize(typeof(List<UserInfo>), s) as List<UserInfo>;
foreach (var info in newList)
{
Console.WriteLine(info.UserId.ToString() + ", " + info.UserName);
}
}
}
}
输出结果为:
C:\WINDOWS\system32\cmd.exe 复制 保存
<?
xml version
=
"
1.0
"
encoding
=
"
utf-16
"
?>
<
UserInfo xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xmlns:xsd
=
"
http:
//
www.w3.org/2001/XMLSchema">
<
userId
>
1
</
userId
>
<
userName
>
Guozhijian
</
userName
>
</
UserInfo
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<?
xml version
=
"
1.0
"
encoding
=
"
utf-16
"
?>
<
ArrayOfUserInfo xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xmlns:xsd
=
"
http://www.w3.org/2001/XMLSchema
"
>
<
UserInfo
>
<
userId
>
1
</
userId
>
<
userName
>
Guozhijian
</
userName
>
</
UserInfo
>
<
UserInfo
>
<
userId
>
2
</
userId
>
<
userName
>
Zhenglanzhen
</
userName
>
</
UserInfo
>
</
ArrayOfUserInfo
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
1
, Guozhijian
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
1
, Guozhijian
2
, Zhenglanzhen