操作xml文档的通用方法
来源:互联网 发布:linux 进程启动时间 编辑:程序博客网 时间:2024/06/05 16:15
Demo
public class XmlSerializeUtils
{
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
/// <summary>
/// 获取指定类型的 XmlSerializer
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static XmlSerializer GetSerializer(Type t)
{
int type_hash = t.GetHashCode();
if (!serializer_dict.ContainsKey(type_hash))
serializer_dict.Add(type_hash, new XmlSerializer(t));
return serializer_dict[type_hash];
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static T Load<T>(string filename)
{
Type type=typeof(T);
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return (T)serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static bool Save(object obj, string filename)
{
bool success = false;
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
success = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
return success;
}
/// <summary>
/// xml序列化成字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>xml字符串</returns>
public static string Serialize(object obj)
{
string returnStr = "";
XmlSerializer serializer = GetSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = null;
StreamReader sr = null;
try
{
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = System.Xml.Formatting.Indented;
serializer.Serialize(xtw, obj);
ms.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(ms);
returnStr = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (xtw != null)
xtw.Close();
if (sr != null)
sr.Close();
ms.Close();
}
return returnStr;
}
/// <summary>
/// 从字符串反序列化为对象
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static T DeSerialize<T>(string s)
{
Type type = typeof(T);
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
try
{
XmlSerializer serializer = GetSerializer(type);
return (T)serializer.Deserialize(new MemoryStream(b));
}
catch (Exception ex)
{
throw ex;
}
}
}
{
private static Dictionary<int, XmlSerializer> serializer_dict = new Dictionary<int, XmlSerializer>();
/// <summary>
/// 获取指定类型的 XmlSerializer
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static XmlSerializer GetSerializer(Type t)
{
int type_hash = t.GetHashCode();
if (!serializer_dict.ContainsKey(type_hash))
serializer_dict.Add(type_hash, new XmlSerializer(t));
return serializer_dict[type_hash];
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="filename">文件路径</param>
/// <returns></returns>
public static T Load<T>(string filename)
{
Type type=typeof(T);
FileStream fs = null;
try
{
// open the stream...
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(type);
return (T)serializer.Deserialize(fs);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
}
/// <summary>
/// 序列化
/// </summary>
/// <param name="obj">对象</param>
/// <param name="filename">文件路径</param>
public static bool Save(object obj, string filename)
{
bool success = false;
FileStream fs = null;
// serialize it...
try
{
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlSerializer serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(fs, obj);
success = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (fs != null)
fs.Close();
}
return success;
}
/// <summary>
/// xml序列化成字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>xml字符串</returns>
public static string Serialize(object obj)
{
string returnStr = "";
XmlSerializer serializer = GetSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = null;
StreamReader sr = null;
try
{
xtw = new System.Xml.XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting = System.Xml.Formatting.Indented;
serializer.Serialize(xtw, obj);
ms.Seek(0, SeekOrigin.Begin);
sr = new StreamReader(ms);
returnStr = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (xtw != null)
xtw.Close();
if (sr != null)
sr.Close();
ms.Close();
}
return returnStr;
}
/// <summary>
/// 从字符串反序列化为对象
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static T DeSerialize<T>(string s)
{
Type type = typeof(T);
byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
try
{
XmlSerializer serializer = GetSerializer(type);
return (T)serializer.Deserialize(new MemoryStream(b));
}
catch (Exception ex)
{
throw ex;
}
}
}
1 0
- 操作xml文档的通用方法
- c#操作xml文档的通用类
- c#操作XML文件的通用方法
- c#操作XML文件的通用方法
- c#操作XML文件的通用方法
- 对XML文档操作的通用CRUD(JDOM版)
- 常用的操作xml文档的方法
- XML文档的操作
- 通用的XML处理方法
- c# 操作xml一般通用的范例
- c# 操作xml一般通用的范例
- asp.net的XML通用操作类
- asp.net的XML通用操作类
- asp.net的XML通用操作类
- asp.net的XML通用操作类
- 对寄存器操作的通用方法总结
- 对寄存器操作的通用方法总结
- 对寄存器操作的通用方法总结
- Js拖动特效,一串水晶球(很强大)
- 工作五年总结
- Android Studio中设置作者和创建日期等注释模板
- ERROR:transport error 202: gethostbyname: unknown host
- 深入JVM锁机制-synchronized
- 操作xml文档的通用方法
- Exception in thread “commons-pool-EvictionTimer”
- 用文件函数获取文件的大小代码片段
- 算法系列(十三)图论基本概念和拓扑排序
- HYSBZ 1036 树链剖分
- hdu2087
- jsp与servlet传值相关问题
- 【剑指offer】二叉搜索树与双向链表
- Rabbitmq 学习笔记(二)队列