以下是一个包装的用于序列化反序列化XML和C# 对象的类。
public class XmlSerializeHelper<T>
{
#region Serialize/Deserialize
private static System.Xml.Serialization.XmlSerializer serializer;
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
}
return serializer;
}
}
/// <summary>
/// Serializes object into an XML document
/// </summary>
/// <returns>string XML value</returns>
public virtual string Serialize()
{
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
try
{
memoryStream = new System.IO.MemoryStream();
Serializer.Serialize(memoryStream, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
catch
{
}
finally
{
if ((streamReader != null))
{
streamReader.Dispose();
}
if ((memoryStream != null))
{
memoryStream.Dispose();
}
}
return "";
}
/// <summary>
/// Deserialize xml string into an object
/// </summary>
/// <param name="xml">string to deserialize</param>
/// <param name="obj">Output object</param>
/// <param name="exception">output Exception value if deserialize failed</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool Deserialize(string xml, out T obj, out System.Exception exception)
{
exception = null;
obj = default(T);
try
{
obj = Deserialize(xml);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
/// <summary>
/// Deserialize xml string into an object
/// </summary>
/// <param name="xml">string to deserialize</param>
/// <param name="obj">Output object</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool Deserialize(string xml, out T obj)
{
System.Exception exception = null;
return Deserialize(xml, out obj, out exception);
}
/// <summary>
/// Deserialize xml file into construct.
/// </summary>
/// <param name="xml">string to deserialize</param>
/// <returns>object if this XmlSerializer can deserialize the object</returns>
public static T Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((T)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
catch
{
//
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
return default(T);
}
/// <summary>
/// Serializes current object into file
/// </summary>
/// <param name="fileName">full path of output xml file</param>
/// <param name="exception">output Exception value if failed</param>
/// <returns>true if can serialize and save into file; otherwise, false</returns>
public virtual bool SaveToFile(string fileName, out System.Exception exception)
{
exception = null;
try
{
SaveToFile(fileName);
return true;
}
catch (System.Exception e)
{
exception = e;
return false;
}
}
/// <summary>
/// Serializes current object into file
/// </summary>
/// <param name="fileName">full path of output xml file</param>
/// <returns>true if can serialize and save into file; otherwise, false</returns>
public virtual void SaveToFile(string fileName)
{
System.IO.StreamWriter streamWriter = null;
try
{
string xmlString = Serialize();
System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
streamWriter = xmlFile.CreateText();
streamWriter.WriteLine(xmlString);
streamWriter.Close();
}
catch
{
//
}
finally
{
if ((streamWriter != null))
{
streamWriter.Dispose();
}
}
}
/// <summary>
/// Deserialize xml markup from file into an object
/// </summary>
/// <param name="fileName">string xml file to load and deserialize</param>
/// <param name="obj">Output object</param>
/// <param name="exception">output Exception value if deserialize failed</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool LoadFromFile(string fileName, out T obj, out System.Exception exception)
{
exception = null;
obj = default(T);
try
{
obj = LoadFromFile(fileName);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
/// <summary>
/// Deserialize xml markup from file into an object
/// </summary>
/// <param name="fileName">string xml file to load and deserialize</param>
/// <param name="obj">Output object</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool LoadFromFile(string fileName, out T obj)
{
System.Exception exception = null;
return LoadFromFile(fileName, out obj, out exception);
}
/// <summary>
/// Deserialize xml markup from file into an object
/// </summary>
/// <param name="fileName">string xml file to load and deserialize</param>
/// <returns></returns>
public static T LoadFromFile(string fileName)
{
System.IO.FileStream file = null;
System.IO.StreamReader sr = null;
try
{
file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
sr = new System.IO.StreamReader(file);
string xmlString = sr.ReadToEnd();
sr.Close();
file.Close();
return Deserialize(xmlString);
}
catch
{
}
finally
{
if ((file != null))
{
file.Dispose();
}
if ((sr != null))
{
sr.Dispose();
}
}
return default(T);
}
#endregion
}
- 学习C# XmlSerializer 序列化反序列化XML
类.变量常用头: [XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类 [XmlType]:对节点描述,在类声明中使用 如:下例的Head类 [X ...
- 05-06 Flutter JSON和序列化反序列化、创建模型类转换Json数据、轮播图数据渲染:Flutter创建商品数据模型 、请求Api接口渲染热门商品 推荐商品
Config.dart class Config{ static String domain='http://jd.itying.com/'; } FocusModel.dart class Focu ...
- c# XML和实体类之间相互转换(序列化和反序列化)[砖]
link: http://blog.okbase.net/haobao/archive/62.html by: 好饱 我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlU ...
- C# XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...
- XML序列化反序列化—常用类
public class XMLSerializer { #region (public) xml序列化 /// <summary> /// ...
- XML和实体类之间相互转换(序列化和反序列化)
我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- 用C#实现XML和实体类之间序列化和反序列化相互转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 对类参数的序列化和反序列化XML
/// <summary> /// Xml序列化与反序列化 /// </summary> public class XmlUtil { #region 反序列化 /// < ...
- php json与xml序列化/反序列化
在web开发中对象的序列化与反序列化经常使用,比较主流的有json格式与xml格式的序列化与反序列化,今天想写个jsop的小demo,结果发现不会使用php序列化,查了一下资料,做个笔记 简单数组js ...
随机推荐
- 过滤XSS的HTMLPurifier使用
什么是HTMLPurifier? 在php里解决XSS最简单的方法是使用htmlspecialchars转义xml实体,但对于需要使用xml的时候就搏手无策了. HTML Purifier是基于php ...
- 【转】【Axure学习】之短信动态验证码+图片动态验证码
感谢:努力拼搏的80后的<巧用Axure三步轻松搞定图片验证码>. 人人都是产品经理的<Axure 教程:实现倒计时获取验证码效果>
- SkipList跳跃表(Java实现)
取自网络https://github.com/spratt/SkipList AbstractSortedSet.java package skiplist_m; /***************** ...
- 多媒体开发之---h264 取流解码实现
解码器在解码时,首先逐个字节读取NAL的数据,统计NAL的长度,然后再开始解码. nal_unit( NumBytesInNALunit ) { /* NumBytesInNALunit为统计出来的 ...
- 多媒体开发之---如何确定slice_header slice_type 的位置
引用网友的问答:我找到0x000001 NAL的开头了,请问如何确定slice head的位置,继而得出slice_type呢?Nal unit后紧跟的就是slice head吗?标准里的循环让人看得 ...
- POJ2407_Relatives【欧拉phi函数】【基本】
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11422 Accepted: 5571 Descriptio ...
- java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000
在操作SQL中存在In的数量如果超过1000条会提示 ORA-01795: 列表中的最大表达式数为 1000 归纳有几种方式出现的: 第一种是:我在上一个 [jdbc 同时执行 查询和删除操]作中 ...
- js中scrollLeft、scrollWidth、offsetTop等相关位置属性的理解(转)
1.常见的事件位置属性 e.pageX——相对整个页面的坐标 注意:IE6.IE7.IE8无该属性 e.layerX——相对当前坐标系的border左上角开始的坐标 注意:在opera.IE6.IE7 ...
- 【BZOJ2597】[Wc2007]剪刀石头布 最小费用流
[BZOJ2597][Wc2007]剪刀石头布 Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之 ...
- android菜鸟学习笔记9----Activity(二)
关于Activity的生命周期: 下面是Activity整个生命周期中,状态发生变化时所回调的方法,它们对应着Activity完整的生命过程. void onCreate(Bundle savedI ...