xml序列化与反序列化工具
发布时间:2020-12-16 23:46:06 所属栏目:百科 来源:网络整理
导读:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml.Serialization; using System.Xml; namespace ConsoleApplication1{ /// summary /// xml序列化与反序列化工具 /// /summary pu
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml.Serialization; using System.Xml; namespace ConsoleApplication1 { /// <summary> /// xml序列化与反序列化工具 /// </summary> public class XmlSerializeUtil { /// <summary> /// xml反序列化 /// </summary> /// <param name="type"></param> /// <param name="xml"></param> /// <example> /// CustomerMapPageRoute customerRoute = XmlSerializeUtil.Deserialize(typeof(CustomerMapPageRoute),xmldoc.OuterXml) as CustomerMapPageRoute /// </example> /// <returns></returns> public static object Deserialize(Type type,string xml) { using (StringReader stringReader = new StringReader(xml)) { XmlSerializer xmlSerializer = new XmlSerializer(type); return xmlSerializer.Deserialize(stringReader); } } /// <summary> /// xml反序列化 /// </summary> /// <param name="type"></param> /// <param name="stream"></param> /// <returns></returns> public static object Deserialize(Type type,Stream stream) { XmlSerializer xmlSerializer = new XmlSerializer(type); return xmlSerializer.Deserialize(stream); } /// <summary> /// xml反序列化泛型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xml"></param> /// <example> /// CustomerMapPageRoute obj1 = XmlSerializeUtil.Deserialize<CustomerMapPageRoute>(xmldoc.OuterXml); /// </example> /// <returns></returns> public static T Deserialize<T>(string xml) { using (StringReader stringReader = new StringReader(xml)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(stringReader); } } /// <summary> /// xml反序列化泛型 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="type"></param> /// <param name="stream"></param> /// <returns></returns> public static T Deserialize<T>(Type type,Stream stream) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(stream); } /// <summary> /// 对象序列化为xml /// </summary> /// <param name="type"></param> /// <param name="obj"></param> /// <example> /// string xml = XmlSerializeUtil.Serializer(typeof(对象名),obj); /// </example> /// <returns></returns> public static string Serializer(Type type,object obj) { try { using (MemoryStream stream = new MemoryStream()) { XmlSerializer xmlSerializer = new XmlSerializer(type); xmlSerializer.Serialize(stream,obj); stream.Position = 0; using (StreamReader streamReader = new StreamReader(stream)) { return streamReader.ReadToEnd(); } } } catch (Exception ex) { throw ex; } } /// <summary> /// 对象序列化为xml /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="t">对象</param> /// <returns>xml encoding="utf-8"</returns> /// <example> /// string xml = XmlSerializeUtil.Serializer<对象名>(obj); //encoding="utf-8" /// </example> public static string Serializer<T>(T t) { return Serializer<T>(t,Encoding.UTF8); } /// <summary> /// 对象序列化为xml /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="t">对象</param> /// <param name="encoding">Encoding e = Encoding.UTF8 ASCII,Unicode,UTF32,UTF7,UTF8,Default /// Encoding.Default <?xml version="1.0" encoding="gb2312"?> /// Encoding.UTF8 <?xml version="1.0" encoding="utf-8"?> /// </param> /// <example> /// string xml = XmlSerializeUtil.Serializer<对象名>(obj); //encoding="utf-8" /// string xml = XmlSerializeUtil.Serializer<对象名>(obj,Encoding.Default); //encoding="gb2312" /// </example> /// <returns>xml</returns> public static string Serializer<T>(T t,Encoding encoding) { if (t == null) throw new ArgumentException("object is null"); if (encoding == null) { encoding = Encoding.UTF8; } try { using(MemoryStream stream=new MemoryStream()) { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Encoding = encoding; XmlWriter xmlWiter = XmlWriter.Create(stream,xmlWriterSettings); XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); xsn.Add(string.Empty,string.Empty); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); xmlSerializer.Serialize(xmlWiter,t,xsn); xmlWiter.Close(); stream.Position = 0; using(StreamReader streamReader=new StreamReader(stream,encoding)) { return streamReader.ReadToEnd(); } } } catch(Exception ex) { throw ex; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ExtJs中Ext.Ajax.request()跟getForm().submit()得用法
- reactjs – 如何使用react-router和ES6类模拟window.locati
- 转载只为记录经典之<NAND和NOR flash的区别>
- [Swift Weekly Contest 108]LeetCode929. 独特的电子邮件地
- 半小时了解正则表达式
- ios – COCOAPODS验证i386错误
- jsonp jquery jersey 实现跨域访问
- postgresql – Postgres – 计算累积数据的变化
- 在实现类中注入存储库而不使用xml在spring数据中使用@autow
- 用于更改xml元素的shell脚本[复制]