C#读取XML文件,反序列化为指定对象

来源:互联网 发布:乐乎lofter4.9.0 编辑:程序博客网 时间:2024/06/05 22:53

Xml序列化帮助类:

    public class XmlSerializeHelper    {        public static string Serialize<T>(T obj)        {            return Serialize<T>(obj, Encoding.UTF8);        }        /// <summary>        /// 实体对象序列化成xml字符串        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="obj"></param>        /// <returns></returns>        public static string Serialize<T>(T obj, Encoding encoding)        {            try            {                if (obj == null)                    throw new ArgumentNullException("obj");                var ser = new XmlSerializer(obj.GetType());                using (var ms = new MemoryStream())                {                    using (var writer = new XmlTextWriter(ms, encoding))                    {                        writer.Formatting = Formatting.Indented;                        ser.Serialize(writer, obj);                    }                    var xml = encoding.GetString(ms.ToArray());                    xml = xml.Replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");                    xml = xml.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", "");                    return xml;                }            }            catch (Exception ex)            {                throw ex;            }        }        /// <summary>        /// 反序列化xml字符为对象,默认为Utf-8编码        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="xml"></param>        /// <returns></returns>        public static T DeSerialize<T>(string xml)            where T : new()        {            return DeSerialize<T>(xml, Encoding.UTF8);        }        /// <summary>        /// 反序列化xml字符为对象        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="xml"></param>        /// <param name="encoding"></param>        /// <returns></returns>        public static T DeSerialize<T>(string xml, Encoding encoding)            where T : new()        {            try            {                var mySerializer = new XmlSerializer(typeof(T));                using (var ms = new MemoryStream(encoding.GetBytes(xml)))                {                    using (var sr = new StreamReader(ms, encoding))                    {                        return (T)mySerializer.Deserialize(sr);                    }                }            }            catch (Exception e)            {                return default(T);            }        }    }
xml文档内容:

<?xml version="1.0" encoding="utf-8" ?><SupplierList>  <Supplier name="苏宁易购"  href="http://112.74.98.194/emall/11118_0_0_0_0_0_0_0.html?page="/>  <Supplier name="京东商城"  href="http://112.74.98.194/emall/11111_0_0_0_0_0_0_0.html?page="/>  <Supplier name="国美在线"  href="http://112.74.98.194/emall/11113_0_0_0_0_0_0_0.html?page="/>  <Supplier name="又一朝阳"  href="http://112.74.98.194/emall/11115_0_0_0_0_0_0_0.html?page="/></SupplierList>
对应实体类: 

using System.Xml.Serialization;

    [XmlRoot("SupplierList")]    public class SupplierList    {        [XmlElement("Supplier")]        public List<Supplier> DataSource { get; set; }    }    [XmlType("Supplier")]    public class Supplier    {        [XmlAttribute("href")]        public string Href { get; set; }        [XmlAttribute("name")]        public string Name { get; set; }    }
读取Xml文件,反序列化为指定对象:

    FileStream fs = File.Open("Attach\\Supplier.xml", FileMode.Open, FileAccess.Read);    StreamReader sr = new StreamReader(fs);    string text = sr.ReadToEnd();    var list = XmlSerializeHelper.DeSerialize<SupplierList>(text);

如果要序列化指定的实体类,则需要在类上打[Serializable]标签.





0 0
原创粉丝点击