欢迎光临

木子屋

标题: C# XML序列化/反序列化类XmlSerializer使用示例

作者: dnawo

日期: 2013-05-03 10:55 PM

分类: Win编程

内容: 复制内容到剪贴板 程序代码using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//序列化
Person person = new Person() { Name = "dnawo", Age = 100 };
string xml = Serialize(person);
Console.WriteLine(xml);
//反序列化
person = Deserialize<Person>(xml);
Console.WriteLine(person.Name);
Console.ReadKey();
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string Serialize(object o)
{
return Serialize(o, new UTF8Encoding(false));
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="o"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Serialize(object o, Encoding encode)
{
string result = string.Empty;
//方法1：
using (MemoryStream output = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(o.GetType());
serializer.Serialize(output, o);
result = encode.GetString(output.ToArray());
}
//方法2：
//using (MemoryStream output = new MemoryStream())
//{
// using (XmlTextWriter writer = new XmlTextWriter(output, encode))
// {
// XmlSerializer serializer = new XmlSerializer(o.GetType());
// serializer.Serialize(writer, o);
// }
// result = encode.GetString(output.ToArray());
//}
//方法3：可删除xml声明和默认命名空间(xmlns:xsi,xmlns:xsd)
//XmlWriterSettings setting = new XmlWriterSettings();
//setting.Encoding = encode;
//setting.OmitXmlDeclaration = true;
//XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
//namespaces.Add("", "");
//using (MemoryStream output = new MemoryStream())
//{
// using (XmlWriter writer = XmlWriter.Create(output, setting))
// {
// XmlSerializer serializer = new XmlSerializer(o.GetType());
// serializer.Serialize(writer, o, namespaces);
// result = encode.GetString(output.ToArray());
// }
//}
return result;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <returns></returns>
public static T Deserialize<T>(string str) where T : class
{
return Deserialize<T>(str, new UTF8Encoding(false));
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="str"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static T Deserialize<T>(string str, Encoding encode) where T : class
{
T result = null;
//方法1：
using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
result = serializer.Deserialize(input) as T;
}
//方法2：
//using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
//{
// using (XmlTextReader reader = new XmlTextReader(input))
// {
// XmlSerializer serializer = new XmlSerializer(typeof(T));
// result = serializer.Deserialize(reader) as T;
// }
//}
//方法3：
//using (MemoryStream input = new MemoryStream(encode.GetBytes(str)))
//{
// using (XmlReader reader = XmlReader.Create(input))
// {
// XmlSerializer serializer = new XmlSerializer(typeof(T));
// result = serializer.Deserialize(reader) as T;
// }
//}
return result;
}
}
/// <summary>
/// 测试类
/// </summary>
[XmlRoot("per")]
public class Person
{
[XmlElement("na")]
public string Name { get; set; }
[XmlElement("ag")]
public int Age { get; set; }
}
}
运行结果：
引用内容<?xml version="1.0" encoding="utf-8"?><per xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><na>dnawo</na><ag>100</ag></per>
dnawo
常用Attribute
复制内容到剪贴板 程序代码XmlRootAttribute
XmlElementAttribute
XmlTextAttribute
XmlIgnoreAttribute

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.789 ms

返回

标题: C# XML序列化/反序列化类XmlSerializer使用示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.789 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.836 ms

返回