zoukankan      html  css  js  c++  java
  • C# XML序列化和反序列化

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace SunCreate.ConfirmingSecrecyMS.DistributedService
    {
        public class XmlHelp
        {
    
            /// <summary>
            /// 序列化对象
            /// </summary>
            /// <typeparam name="T">对象类型</typeparam>
            /// <param name="Entity">对象</param>
            /// <param name="Path">xml存放路径</param>
            public static void XmlSerialize<T>(T Entity,string Path) where T:class
            {
                if(string.IsNullOrEmpty(Path))
                {
                    return;
                }
                string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
                string BasePath = System.IO.Path.Combine(Path, "Usual");
                if (!System.IO.Directory.Exists(BasePath))
                {
                    System.IO.Directory.CreateDirectory(BasePath);
                }
                string FileFullPath = System.IO.Path.Combine(BasePath, FileName);
                try
                {
                    using (StreamWriter sw = new StreamWriter(FileFullPath))
                    {
                        XmlSerializer Serializer = new XmlSerializer(typeof(T));
                        Serializer.Serialize(sw, Entity);
                        sw.Close();
                    }
                }
                catch(Exception ex)
                {
                }
            }
    
    
            /// <summary>
            /// 反序列化xml
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="FileFullPath"></param>
            /// <returns></returns>
            public static T XmlDeserialize<T>(string FileFullPath) where T:class
            {
                if (!File.Exists(FileFullPath))
                {
                    return null;
                }
                try
                {
                    using (StreamReader sr = new StreamReader(FileFullPath))
                    {
                        XmlSerializer Serializer = new XmlSerializer(typeof(T));
                        object Result = Serializer.Deserialize(sr) ;
                        sr.Close();
                        return Result as T;
                    }
                }
                catch
                {
                    return null;
                }
            }
        }
    }

    调用方法:

    //序列化
    XmlHelp.XmlSerialize<CatalogInfo>(new CatalogInfo(), this.m_StrRootDirPath); 

    //反序列化
    CatalogInfo clsCataInfo = XmlHelp.XmlDeserialize<CatalogInfo>(
    this.m_StrRootDirPath);
    public class CatalogInfo
    {
    }
  • 相关阅读:
    TestNg线程池配置、执行次数配置、超时配置
    testng.xml文件结构组成及节点属性说明
    ReportNg 测试报告的定制修改【转】
    TestNg依赖详解(三)------灵活的文件配置依赖
    TestNg依赖高级用法之强制依赖与顺序依赖------TestNg依赖详解(二)
    TestNg依赖配置基础用法(单一方法依赖)------TestNg依赖详解(一)
    compareTo,Comparator和equals
    HashMap源码解析
    redis的相关知识
    IO模型
  • 原文地址:https://www.cnblogs.com/SuperMetalMax/p/6187274.html
Copyright © 2011-2022 走看看