• 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;
            }
        }
    }
    

      

  • 相关阅读:
    【设计模式】3、工厂方法模式
    【设计模式】2、生成器模式(建造者模式)
    【设计模式】1、抽象工厂模式
    UNION 和UNION ALL
    树的遍历
    相关前台跨域的解决方式
    有关this指针指向问题
    有关箭头函数
    深入理解js的变量提升和函数提升
    linux tail 命令详解
  • 原文地址:https://www.cnblogs.com/yangzh666/p/13580895.html
Copyright © 2020-2023  润新知