XML---对象系列化与反系列化
发布时间:2020-12-15 23:23:48 所属栏目:百科 来源:网络整理
导读:using System;using System.Collections.Generic;using System.Web;using System.Text;using System.Reflection;using System.Xml;using System.Xml.Serialization;using System.IO;using System.Xml.Schema;namespace Erp.WebClient.Common{ public static
using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Reflection; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Xml.Schema; namespace Erp.WebClient.Common { public static class XmlUtility { #region XML与实体类之间的转换 /// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="toBeIndented"></param> /// <param name="type"></param> /// <returns></returns> public static string ObjectToXml(object obj,bool toBeIndented,Type type) { if (obj == null) { throw new ArgumentNullException("obj"); } UTF8Encoding encoding = new UTF8Encoding(false); XmlSerializer serializer = new XmlSerializer(type); MemoryStream stream = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(stream,encoding); writer.Formatting = (toBeIndented ? Formatting.Indented : Formatting.None); serializer.Serialize(writer,obj); string xml = encoding.GetString(stream.ToArray()); writer.Close(); return xml; } /// <summary> /// /// </summary> /// <param name="xml"></param> /// <param name="type"></param> /// <returns></returns> public static object XmlToObject(string xml,Type type) { if (xml == null) { throw new ArgumentNullException("xml"); } object o = null; XmlSerializer serializer = new XmlSerializer(type); StringReader strReader = new StringReader(xml); XmlReader reader = new XmlTextReader(strReader); try { o = serializer.Deserialize(reader); } catch (InvalidOperationException e) { throw e; } catch (Exception e) { throw e; } finally { reader.Close(); } return o; } /// <summary> /// 实体类序列化成xml /// </summary> /// <param name="enitities">实体.</param> /// <param name="headtag">节点名称</param> /// <returns></returns> public static string ObjectListToXml<T>(List<T> enitities,string headtag) { StringBuilder sb = new StringBuilder(); PropertyInfo[] propinfos = null; sb.AppendLine("<?xml version="1.0" encoding="utf-8"?>"); sb.AppendLine("<" + headtag + ">"); foreach (T obj in enitities) { //初始化propertyinfo if (propinfos == null) { Type objtype = obj.GetType(); propinfos = objtype.GetProperties(); } sb.AppendLine("<ProductList>"); foreach (PropertyInfo propinfo in propinfos) { sb.Append("<"); sb.Append(propinfo.Name); sb.Append(">"); sb.Append(propinfo.GetValue(obj,null)); sb.Append("</"); sb.Append(propinfo.Name); sb.AppendLine(">"); } sb.AppendLine("</ProductList>"); } sb.AppendLine("</" + headtag + ">"); return sb.ToString(); } //xml转行为实体类 /// <summary> /// xml文件转化为实体类列表 /// </summary> /// <typeparam name="T">实体名称</typeparam> /// <param name="xml">xml文件</param> /// <param name="headtag">xml头文件</param> /// <returns>实体列表</returns> public static List<T> XmlToObjectList<T>(string xml,string headtag) where T : new() { List<T> list = new List<T>(); XmlDocument doc = new XmlDocument(); PropertyInfo[] propinfos = null; doc.LoadXml(xml); //XmlNodeList nodelist = doc.SelectNodes(headtag); XmlNodeList nodelist = doc.GetElementsByTagName(headtag); foreach (XmlNode node in nodelist) { T entity = new T(); //初始化propertyinfo if (propinfos == null) { Type objtype = entity.GetType(); propinfos = objtype.GetProperties(); } //填充entity类的属性 foreach (PropertyInfo propinfo in propinfos) { //实体类字段首字母变成小写的 string name = propinfo.Name.Substring(0,1) + propinfo.Name.Substring(1,propinfo.Name.Length - 1); XmlNode cnode = node.SelectSingleNode(name); string v = cnode.InnerText; if (!string.IsNullOrEmpty(v)) { propinfo.SetValue(entity,Convert.ChangeType(v,propinfo.PropertyType),null); } } list.Add(entity); } return list; } #endregion } }
3. 类的设计 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Xml.Serialization; using System.ServiceModel; using System.Runtime.Serialization; namespace GlacierICR.Common.WorkDataTypes { public enum DecimalLimitType { NotSet,MoreThan,MoreThanEqual,LessThan,LessThanEqual,} //[DataContract(Namespace = "http://tempuri.org/")] [XmlInclude(typeof(ClientUIType))] [KnownType(typeof(ClientUIType))] public class ClientUIConfigSetting { public ClientUIConfigSetting() { this.ClientUITypeSetCollection = new List<ClientUIType>(); } public bool HasRemarks { get; set; } public List<ClientUIType> ClientUITypeSetCollection { get; set; } } [XmlInclude(typeof(SingleOption))] [XmlInclude(typeof(MulOption))] [XmlInclude(typeof(DateSelection))] [XmlInclude(typeof(SingleLineTextBox))] [XmlInclude(typeof(MulLineTextBox))] [XmlInclude(typeof(DecimalTextBox))] [KnownType(typeof(SingleOption))] [KnownType(typeof(MulOption))] [KnownType(typeof(DateSelection))] [KnownType(typeof(SingleLineTextBox))] [KnownType(typeof(MulLineTextBox))] [KnownType(typeof(DecimalTextBox))] public abstract class ClientUIType { public string Name { get; set; } } public class SingleOption : ClientUIType { public string[] OptionItems { get; set; } public bool HasOther { get; set; } } public class MulOption : ClientUIType { public string[] OptionItems { get; set; } public bool HasOther { get; set; } } public class DateSelection : ClientUIType { } public class SingleLineTextBox : ClientUIType { public int Length { get; set; } } public class MulLineTextBox : ClientUIType { public int Length { get; set; } } public class DecimalTextBox : ClientUIType { public decimal MinValue { get; set; } public decimal MaxValue { get; set; } public string Unit { get; set; } public DecimalLimitType MinDecimalLimitType { get; set; } public DecimalLimitType MaxDecimalLimitType { get; set; } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |