zoukankan      html  css  js  c++  java
  • [Visual C#] 某保险公司航空意外险非主流XML序列化代码

     1         /// <summary>
     2         /// 将Xml对象转换为指定的实例
     3         /// </summary>
     4         /// <typeparam name="T">实例</typeparam>
     5         /// <param name="xml">Xml字符串</param>
     6         /// <param name="graph">目标对象</param>
     7         /// <param name="ignorProperties">不需要转换的元素集合</param>
     8         /// <param name="isSpecialDateTime">是否为特殊时间(因为航联数据时间比较特殊使用)</param>
     9         /// <returns></returns>
    10         public static T DeserializeXmlToObject<T>(string xml, T graph, string[] ignorProperties,bool isSpecialDateTime)
    11         {
    12             XmlDocument xmlDoc = new XmlDocument();
    13             try
    14             {
    15                 xmlDoc.LoadXml(xml);
    16                 XmlNodeList list = xmlDoc.DocumentElement.ChildNodes;
    17                 Type t = graph.GetType();
    18                 foreach (XmlNode node in list)
    19                 {
    20                     if (Array.Exists<string>(ignorProperties,
    21                      target => (string.Compare(target, node.Name, true) == 0)) == false)
    22                     {
    23                         if (!string.IsNullOrEmpty(node.InnerText))
    24                         {
    25                             PropertyInfo info = null;
    26                             try
    27                             {
    28                                 info = t.GetProperty(node.Name);
    29                             }
    30                             catch
    31                             {
    32                                 continue;
    33                             }
    34                             object val = null;
    35                             if (isSpecialDateTime)
    36                             {
    37                                 if (info.PropertyType.Name.Equals("DateTime") ||
    38                                     info.PropertyType.Name.Equals("System.DateTime"))
    39                                 {
    40                                     string dateTime = node.InnerText.PadRight(14'0');
    41                                     val = DateTime.ParseExact(dateTime, "yyyyMMddHHmmss"null);
    42                                 }
    43                                 else
    44                                 {
    45                                     val = Convert.ChangeType(node.InnerText, info.PropertyType);
    46                                 }
    47                             }
    48                             else
    49                             {
    50                                 val = Convert.ChangeType(node.InnerText, info.PropertyType);
    51                             }
    52                             info.SetValue(graph, val, null);
    53                         }
    54                     }
    55                 }
    56             }
    57             catch (XmlException err)
    58             {
    59                 throw err;
    60             }
    61             catch (Exception err)
    62             {
    63                 throw err;
    64             }
    65             return graph;
    66         }
  • 相关阅读:
    vim复制
    嵌入式Linux学习(二)
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1042 乒乓球
    (Java实现) 洛谷 P1071 潜伏者
    (Java实现) 洛谷 P1071 潜伏者
    (Java实现) 洛谷 P1025 数的划分
    (Java实现)洛谷 P1093 奖学金
    (Java实现)洛谷 P1093 奖学金
    Java实现 洛谷 P1064 金明的预算方案
  • 原文地址:https://www.cnblogs.com/briny/p/2382914.html
Copyright © 2011-2022 走看看