zoukankan      html  css  js  c++  java
  • 通用类 对象和XML互转

     1 public class XMLHealper
     2     {
     3         /// <summary>
     4         /// 将自定义对象序列化为XML字符串
     5         /// </summary>
     6         /// <param name="myObject">自定义对象实体</param>
     7         /// <returns>序列化后的XML字符串</returns>
     8         public static string SerializeToXml<T>(T myObject)
     9         {
    10             if (myObject != null)
    11             {
    12                 XmlSerializer xs = new XmlSerializer(typeof(T));
    13 
    14                 MemoryStream stream = new MemoryStream();
    15                 XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8);
    16                 writer.Formatting = Formatting.None;//缩进
    17                 xs.Serialize(writer, myObject);
    18 
    19                 stream.Position = 0;
    20                 StringBuilder sb = new StringBuilder();
    21                 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
    22                 {
    23                     string line;
    24                     while ((line = reader.ReadLine()) != null)
    25                     {
    26                         sb.Append(line);
    27                     }
    28                     reader.Close();
    29                 }
    30                 writer.Close();
    31                 return sb.ToString();
    32             }
    33             return string.Empty;
    34         }
    35 
    36         /// <summary>
    37         /// 将XML字符串反序列化为对象
    38         /// </summary>
    39         /// <typeparam name="T">对象类型</typeparam>
    40         /// <param name="xmlContent">XML字符</param>
    41         /// <returns></returns>
    42         public static T DeserializeToObject<T>(string xmlContent)
    43         {
    44             T myObject;
    45             XmlSerializer serializer = new XmlSerializer(typeof(T));
    46             StringReader reader = new StringReader(xmlContent);
    47             myObject = (T)serializer.Deserialize(reader);
    48             reader.Close();
    49             return myObject;
    50         }
    51 
    52         public static T RreadXML<T>(string filePath)
    53         {
    54             return DeserializeToObject<T>(FileHelper.ReadFile(filePath));
    55         }
    56         public static bool WriteXML(string xmlContent,string filePath)
    57         {
    58             return FileHelper.WriteFile(xmlContent, filePath);
    59         }
    60         public static bool WriteXML<T>(T contentModel,string filePath)
    61         {
    62             string xmlContent = SerializeToXml<T>(contentModel);
    63             return FileHelper.WriteFile(xmlContent, filePath);
    64         }
    65 
    66     }
  • 相关阅读:
    [BZOJ3751] [NOIP2014] 解方程 (数学)
    [BZOJ4198] [Noi2015] 荷马史诗 (贪心)
    [BZOJ4034] [HAOI2015] T2 (树链剖分)
    [BZOJ1880] [Sdoi2009] Elaxia的路线 (SPFA & 拓扑排序)
    [BZOJ1088] [SCOI2005] 扫雷Mine
    [BZOJ1004] [HNOI2008] Cards (Polya定理)
    [BZOJ1009] [HNOI2008] GT考试 (KMP & dp & 矩阵乘法)
    [BZOJ1503] [NOI2004] 郁闷的出纳员 (treap)
    [BZOJ1059] [ZJOI2007] 矩阵游戏 (二分图匹配)
    BZOJ2626: JZPFAR
  • 原文地址:https://www.cnblogs.com/lsgControl/p/9579757.html
Copyright © 2011-2022 走看看