Xml文件的序列化操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace YYT.IndirectContentService.Core
{
    public class XmlUtil
    {
        /// <summary>
        /// 保存对象为XML到指定路径
        /// </summary>
        public static ResultModel SaveToXml(Object target, String path)
        {
            ResultModel result = new ResultModel();
            try
            {
                XmlSerializer serializer = new XmlSerializer(target.GetType());
                TextWriter textWriter = new StreamWriter(path);
                serializer.Serialize(textWriter, target);
                textWriter.Close();
            }
            catch {
                result.AddErr("系统错误:配置持久化错误!");
            }
            return result;
        }

        /// <summary>
        /// 对象转XML字符串
        /// </summary>
        public static ResultModel ObjectToXml(Object target)
        {
            ResultModel result = new ResultModel();
            try
            {
                StringBuilder sb = new StringBuilder();
                XmlSerializer serializer = new XmlSerializer(target.GetType());
                TextWriter textWriter = new StringWriter(sb);
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                serializer.Serialize(textWriter, target, ns);
                textWriter.Close();
                result.AddInfo("result", sb.ToString());
            }
            catch
            {
                result.AddErr("系统错误:对象转换错误!");
            }
            return result;
        }

        /// <summary>
        /// 加载XML转化为对象
        /// </summary>
        public static T LoadFromXml<T>(String path) where T : class
        {
            object result = null;
            using (StreamReader reader = new StreamReader(path))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                result = xmlSerializer.Deserialize(reader);
            }
            return result as T;
        }


        /// <summary>
        /// 加载XML转化到指定对象
        /// </summary>
        //public static T LoadFromXml<T>(String path, T t) where T : class
        //{
        //    object result = null;
        //    using (StreamReader reader = new StreamReader(path))
        //    {
        //        System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
        //        result = xmlSerializer.Deserialize(reader);
        //    }
        //    t = result as T;
        //    return t;
        //}

        /// <summary>
        /// XML字符串转化为对象
        /// </summary>
        public static T XmlToObject<T>(String str) where T : class
        {
            object result = null;
            using (TextReader reader = new StringReader(str))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                result = xmlSerializer.Deserialize(reader);
            }
            return result as T;
        }

        /// <summary>
        /// XML字符串转化到指定对象
        /// </summary>
        public static T XmlToObject<T>(String str, T t) where T : class
        {
            object result = null;
            using (TextReader reader = new StringReader(str))
            {
                System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                result = xmlSerializer.Deserialize(reader);
            }
            t = result as T;
            return t;
        }

        /// <summary>
        /// 数据库配置保存XML节点属性
        /// </summary>
        public static void SaveDbConnStr(string str)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "DatabaseConfig.xml");//加载xml文件,文件
            XmlElement att = (XmlElement)xmlDoc.ChildNodes[1].FirstChild.FirstChild.FirstChild;
            att.Attributes.Item(1).Value = str;
            xmlDoc.Save("DatabaseConfig.xml");
        }

        /// <summary>
        /// 数据库配置读取XML节点属性
        /// </summary>
        public static string DbConnStr()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "DatabaseConfig.xml");//加载xml文件,文件
            XmlElement att = (XmlElement)xmlDoc.ChildNodes[1].FirstChild.FirstChild.FirstChild;
            return att.Attributes.Item(1).Value;
        }
    }
}

  

posted @ 2020-08-29 09:02  土豆2号  阅读(188)  评论(0编辑  收藏  举报