C# 序列化xml数据到class
2017-06-19 09:12
281 查看
public static class XmlClassData
{
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为 null 的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this FileInfo fileInfo) where C : class
{
return ReadDataFromXml<C>(fileInfo.FullName);
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// /// <param name="FullPath"> 包含数据的文件路径</param>
/// <returns> Tpye = C , 返回为null的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this string FullPath) where C : class
{
return XmlAction.Read(FullPath, typeof(C)) as C;
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="stream">包含数据的数据流 </param>
/// <returns> Tpye = C , 返回为null的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this Stream stream) where C : class
{
return XmlAction.Read(stream, typeof(C)) as C;
}
/// <summary> 写入数据到 XML </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="obj"> 将要写入的数据</param>
/// <param name="fullpath"> 写 =入的文件路径 </param>
/// <returns> 返回为 null 的时候写入成功 </returns>
public static string WriteDataToXml<C>(this C obj, string fullpath) where C : class
{
try
{
XmlAction.Save(obj, fullpath);
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
public static Stream WriteDataToStream<C>(this C obj) where C : class
{
try
{
return XmlAction.SaveStream(obj);
}
catch
{
}
return null;
}
}
/// <summary> struct 类型进行读取时,不确保一定成功 如果读取不成功会返回 default(S) </summary>
public static class XmlStructData
{
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this FileInfo fileInfo) where S : struct
{
return ReadStructFromXml<S>(fileInfo.FullName);
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this string FullPath) where S : struct
{
var data = XmlAction.Read(FullPath, typeof(S));
return data != null ? (S)data : default(S);
}
/// <summary> 从XML读取数据 ,返回为默认值的时候读取失败 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="FullPath"> 读取的路径 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this Stream stream) where S : struct
{
var data = XmlAction.Read(stream, typeof(S));
return data != null ? (S)data : default(S);
}
/// <summary> 写入数据到XML </summary>
/// <typeparam name="S"> 写入的数据类型 </typeparam>
/// <param name="obj"></param>
/// <param name="fullpath"> 写入的路径 </param>
/// <returns> 返回为 null 的时候写入成功 </returns>
public static string WriteStructToXml<S>(this S obj, string fullpath) where S : struct
{
try
{
XmlAction.Save(obj, fullpath);
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
/// <summary> 封装对与Xml读写操作的类 </summary>
internal static class XmlAction
{
internal static void Save(object obj, string filePath)
{
Save(obj, filePath, obj.GetType());
}
internal static void Save(object obj, string filePath, System.Type type)
{
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
var xs = new XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
catch { }
}
internal static Stream SaveStream(object obj)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
var xs = new XmlSerializer(obj.GetType());
xs.Serialize(writer, obj);
writer.Close();
return stream as Stream;
}
internal static object Read(string filePath, System.Type type)
{
if (!System.IO.File.Exists(filePath))
{
return null;
}
try
{
using (StreamReader reader = new StreamReader(filePath))
{
var xs = new XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
catch { }
return null;
}
internal static object Read(Stream stream, System.Type type)
{
if (stream == null || stream.Length == 0)
{
return null;
}
try
{
var xs = new XmlSerializer(type);
object obj = xs.Deserialize(stream);
return obj;
}
catch { }
return null;
}
}
{
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为 null 的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this FileInfo fileInfo) where C : class
{
return ReadDataFromXml<C>(fileInfo.FullName);
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// /// <param name="FullPath"> 包含数据的文件路径</param>
/// <returns> Tpye = C , 返回为null的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this string FullPath) where C : class
{
return XmlAction.Read(FullPath, typeof(C)) as C;
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="stream">包含数据的数据流 </param>
/// <returns> Tpye = C , 返回为null的时候读取失败 </returns>
public static C ReadDataFromXml<C>(this Stream stream) where C : class
{
return XmlAction.Read(stream, typeof(C)) as C;
}
/// <summary> 写入数据到 XML </summary>
/// <typeparam name="C"> 读取的数据类型 </typeparam>
/// <param name="obj"> 将要写入的数据</param>
/// <param name="fullpath"> 写 =入的文件路径 </param>
/// <returns> 返回为 null 的时候写入成功 </returns>
public static string WriteDataToXml<C>(this C obj, string fullpath) where C : class
{
try
{
XmlAction.Save(obj, fullpath);
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
public static Stream WriteDataToStream<C>(this C obj) where C : class
{
try
{
return XmlAction.SaveStream(obj);
}
catch
{
}
return null;
}
}
/// <summary> struct 类型进行读取时,不确保一定成功 如果读取不成功会返回 default(S) </summary>
public static class XmlStructData
{
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this FileInfo fileInfo) where S : struct
{
return ReadStructFromXml<S>(fileInfo.FullName);
}
/// <summary> 从XML读取数据 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="fileInfo"> 包含数据的文件 FileInfo 信息 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this string FullPath) where S : struct
{
var data = XmlAction.Read(FullPath, typeof(S));
return data != null ? (S)data : default(S);
}
/// <summary> 从XML读取数据 ,返回为默认值的时候读取失败 </summary>
/// <typeparam name="S"> 读取的数据类型 </typeparam>
/// <param name="FullPath"> 读取的路径 </param>
/// <returns> 返回为默认值的时候读取失败 </returns>
public static S ReadStructFromXml<S>(this Stream stream) where S : struct
{
var data = XmlAction.Read(stream, typeof(S));
return data != null ? (S)data : default(S);
}
/// <summary> 写入数据到XML </summary>
/// <typeparam name="S"> 写入的数据类型 </typeparam>
/// <param name="obj"></param>
/// <param name="fullpath"> 写入的路径 </param>
/// <returns> 返回为 null 的时候写入成功 </returns>
public static string WriteStructToXml<S>(this S obj, string fullpath) where S : struct
{
try
{
XmlAction.Save(obj, fullpath);
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
/// <summary> 封装对与Xml读写操作的类 </summary>
internal static class XmlAction
{
internal static void Save(object obj, string filePath)
{
Save(obj, filePath, obj.GetType());
}
internal static void Save(object obj, string filePath, System.Type type)
{
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
var xs = new XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
catch { }
}
internal static Stream SaveStream(object obj)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
var xs = new XmlSerializer(obj.GetType());
xs.Serialize(writer, obj);
writer.Close();
return stream as Stream;
}
internal static object Read(string filePath, System.Type type)
{
if (!System.IO.File.Exists(filePath))
{
return null;
}
try
{
using (StreamReader reader = new StreamReader(filePath))
{
var xs = new XmlSerializer(type);
object obj = xs.Deserialize(reader);
reader.Close();
return obj;
}
}
catch { }
return null;
}
internal static object Read(Stream stream, System.Type type)
{
if (stream == null || stream.Length == 0)
{
return null;
}
try
{
var xs = new XmlSerializer(type);
object obj = xs.Deserialize(stream);
return obj;
}
catch { }
return null;
}
}
相关文章推荐
- 【C#】解决进行反序列化时出错:。InnerException 消息是“反序列化对象 属于类型 System.String 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- HOW TO:在 Visual C# .NET 中使用 System.Xml 合并两个 XML 文档中的数据
- 311530 HOW TO:在 Visual C# .NET 中使用 System.Xml 合并两个 XML 文档中的数据 (From MKBA)
- C#中DataGrid加载XML数据--管理 传值到例外个页面 再传值
- C#源码学习之---将数据库数据以XML文件格式保存
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- 在Visual C# .Net 中怎样向Microsoft Excel 2002 传输XML数据[译]
- C#中创建与读取XML数据(一)
- C#中DataGrid加载XML数据--在另外一页面修改数据
- C#中DataGrid加载XML数据--删除数据
- C# 對象到XML的序列化和反序列化
- 使用 Visual C# .NET 向 Microsoft Excel 2002 传输 XML 数据
- C#中XML序列化和反序列化初探
- [转] C#下 读取xml节点的数据总结
- C#中DataGrid加载XML数据--添加数据
- 使用 Visual C# .NET 向 Microsoft Excel 2002 传输 XML 数据
- 用C#接口实现通用的文本数据序列化
- XML 2 Class [xml 转化为 序列化代码工具]
- 转] C#下 读取xml节点的数据总结