Xml操作辅助类主要包括两大操作,Xml的序列化与反序列化。代码如下:

namespace Tmac.Utilities
{
    /// <summary>
    /// Xml操作类
    /// </summary>
    public class XmlUtil
    {
        /// <summary>
        /// 序列化对象为xml(或字节流)
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string XmlSerialize(object obj)
        {
            string result = null;

            try
            {
                XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, new UTF8Encoding(false));
                    xmlTextWriter.Formatting = Formatting.Indented;
                    xmlSerializer.Serialize(xmlTextWriter, obj);
                    xmlTextWriter.Flush();
                    xmlTextWriter.Close();
                    UTF8Encoding uTF8Encoding = new UTF8Encoding(false, true);
                    result = uTF8Encoding.GetString(stream.ToArray());
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Couldn't serialize object:"+obj.GetType().Name,ex);
            }
            return result;
        }

        /// <summary>
        /// 反序列xml为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(Type type,string xmlPath)
        { 
            T obj=default(T);

            XmlSerializer xmlSerializer = new XmlSerializer(type);
            try
            {
                using (StreamReader sr = new StreamReader(xmlPath))
                {
                    obj=(T)xmlSerializer.Deserialize(sr);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Couldn't parse xml:{0};Type:{1}",xmlPath,type.FullName), ex);
            }
            return obj;
        }
    }
}

 

posted on 2013-03-19 23:14  永远的麦子  阅读(3715)  评论(0编辑  收藏  举报