zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
    

      

  • 相关阅读:
    IDA 动态调试 ELF 文件
    双机调试环境搭建[win7+Windbg+VirtualKD]
    从域环境搭建到 MS14-068 提升为域权限
    栈溢出原理与 shellcode 开发
    Flask_0x05 大型程序结构
    rest-framework 框架的基本组件
    restful 规范
    Django的CBV和FBV
    Django-model 进阶
    Django-Ajax
  • 原文地址:https://www.cnblogs.com/yangzh666/p/13580895.html
Copyright © 2011-2022 走看看