C#反序列化:xml转化为实体

2021-05-05 15:27

阅读:559

标签:turn   color   class   utf8   ldo   反序   object   dispose   bsp   

 public static T DeserialXmlToModel(string xmlDocument)
        {
            T cmdObj = default(T);
            XmlSerializer se = new XmlSerializer(typeof(T));
            MemoryStream ms = new 
            MemoryStream(Encoding.UTF8.GetBytes(xmlDocument));
            XmlTextReader reader = new XmlTextReader(ms);
            try
            {
                
                reader.Normalization = false;
                object obj = se.Deserialize(reader);
                if (obj != null)
                {
                    cmdObj = (T)obj;
                }
                reader.Close();
                ms.Close();
            }
            catch (XmlException ex)
            {

            }
            catch (Exception ex)
            {

            }
            finally
            {
                se = null;
                ms.Dispose();
                ms = null;
                reader = null;
            }
            return cmdObj;
}

 

C#反序列化:xml转化为实体

标签:turn   color   class   utf8   ldo   反序   object   dispose   bsp   

原文地址:http://www.cnblogs.com/bincoding/p/7679920.html


评论


亲,登录后才可以留言!