AI智能
改变未来

c#实体和XML集合(数组)格式序列化及反序列化转换


c# 复杂Xml序列化和反序列化相关特性使用

1、序列化及反序列化方法

/// <summary>/// 对象转xml/// </summary>/// <param name=\"obj\"></param>/// <param name=\"encoding\"></param>/// <returns></returns>public static string ToXml(this object obj, string encoding = \"UTF-8\"){XmlWriterSettings xmlWriterSettings = new XmlWriterSettings{Encoding = Encoding.GetEncoding(encoding)};using (var s = new MemoryStream()){using (var xmlWriter = XmlWriter.Create(s, xmlWriterSettings)){var xns = new XmlSerializerNamespaces();xns.Add(\"\", \"\");new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, xns);s.Position = 0;}using (var sr = new StreamReader(s))`{return sr.ReadToEnd();}}}/// <summary>/// Xml转对象/// </summary>/// <typeparam name=\"T\"></typeparam>/// <param name=\"obj\"></param>/// <returns></returns>public static T X2Entity<T>(this string obj) where T : class{try{using (StringReader sr = new StringReader(obj)){XmlSerializer xmldes = new XmlSerializer(typeof(T));return xmldes.Deserialize(sr) as T;}}catch (Exception ex){throw new Exception(\"无效的XML串:\" + ex.Message);}}

**2、新建实体类如下:

public class BaseRes{/// <summary>/// 业务响应码/// </summary>public string TranCode { get; set; }/// <summary>/// 交易结果(0:成功;1:错误)/// </summary>public string ResultCode { get; set; }/// <summary>/// 返回信息/// </summary>public string ErrorMsg { get; set; }}[XmlRoot(\"Response\")]public class RegisterDeptRes : BaseRes{[XmlArray(\"Items\")][XmlArrayItem(\"Item\")]public List<RegisterDeptDetail> Items { get; set; }public RegisterDeptRes(){Items = new List<RegisterDeptDetail>();}}public class RegisterDeptDetail{/// <summary>/// 科室ID/// </summary>[XmlAttribute(\"Code\")]public string DepCode { get; set; }/// <summary>/// 科室名称/// </summary>public string DepName { get; set; }/// <summary>/// 科室分组名称(针对二级科室的情况,填写上级科室名称若只有一级科室,则填写“无”)/// </summary>public string DepGroupName { get; set; }/// <summary>/// 科室分组编号(针对二级科室的情况,填写上级科室编号若只有一级科室,则编号为0)/// </summary>public string DepGroupCode { get; set; }/// <summary>/// 使用人群(0:全部;1:儿童;2:成人;5:老年)/// </summary>public string UserType { get; set; }/// <summary>/// 号源标识(1:有号;2:无号)/// </summary>public string NumInd { get; set; }/// <summary>/// 科室简介/// </summary>public string DepDesc { get; set; }}

3、执行操作

序列化:var reqRegisterDept = new RegisterDeptRes(){ResultCode=\"0\",ErrorMsg=\"成功\",TranCode=\"11002\",Items=new List<RegisterDeptDetail>(){new RegisterDeptDetail(){DepCode=\"101\",DepName=\"产房\",DepGroupName=\"病区科室\",DepGroupCode=\"1\",UserType=\"1\",NumInd=\"2\",DepDesc=\"病区科室说明\"},new RegisterDeptDetail(){DepCode=\"102\",DepName=\"康复病区\",DepGroupName=\"病区科室\",DepGroupCode=\"1\",UserType=\"0\",NumInd=\"3\",DepDesc=\"病区科室说明\"}}};var reqXml = reqRegisterDept.ToXml();

得到结果如下:

<?xml version=\"1.0\" encoding=\"utf-8\"?><Response><TranCode>11002</TranCode><ResultCode>0</ResultCode><ErrorMsg>成功</ErrorMsg><Items><Item Code=\"101\"><DepName>产房</DepName><DepGroupName>病区科室</DepGroupName><DepGroupCode>1</DepGroupCode><UserType>1</UserType><NumInd>2</NumInd><DepDesc>病区科室说明</DepDesc></Item><Item Code=\"102\"><DepName>康复病区</DepName><DepGroupName>病区科室</DepGroupName><DepGroupCode>1</DepGroupCode><UserType>0</UserType><NumInd>3</NumInd><DepDesc>病区科室说明</DepDesc></Item></Items></Response>

反序列化操作直接用以上XML字符串转换:

var resDept = str.X2Entity<RegisterDeptRes>();

4、当需要转成的格式是下面这种list型

<?xml version=\"1.0\" encoding=\"utf-8\"?><Response><TranCode>11002</TranCode><ResultCode>0</ResultCode><ErrorMsg>成功</ErrorMsg><Item Code=\"101\"><DepName>产房</DepName><DepGroupName>病区科室</DepGroupName><DepGroupCode>1</DepGroupCode><UserType>1</UserType><NumInd>2</NumInd><DepDesc>病区科室说明</DepDesc></Item><Item Code=\"102\"><DepName>康复病区</DepName><DepGroupName>病区科室</DepGroupName><DepGroupCode>1</DepGroupCode><UserType>0</UserType><NumInd>3</NumInd><DepDesc>病区科室说明</DepDesc></Item></Response>

需要将对象特性做做修改如下:

[XmlRoot(\"Response\")]public class RegisterDeptRes : BaseRes{//[XmlArray(\"Items\")]//[XmlArrayItem(\"Item\")][XmlElement(\"Item\")]public List<RegisterDeptDetail> Items { get; set; }public RegisterDeptRes(){Items = new List<RegisterDeptDetail>();}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » c#实体和XML集合(数组)格式序列化及反序列化转换