c# 对序列化类XMLSerializer 二次封装泛型化方便了一些使用的步骤

  • Post category:C#

 

 

 

  原文作者:aircraft

  原文链接:https://www.cnblogs.com/DOMLX/p/17270107.html

 

 

加工的泛型类如下:

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Data
{
    public class XMLSerializer<T>
    {

        public static bool Save(T obj, string flieName)
        {
            string dir = Path.GetDirectoryName(flieName);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            try
            {
                if (flieName.Trim().Length == 0)
                    return false;
                string strFolder = Path.GetDirectoryName(flieName);

                XmlSerializer xs = new XmlSerializer(typeof(T));
                using (FileStream fs = new FileStream(flieName, FileMode.Create))
                {
                    xs.Serialize(fs, obj);
                    //fs.Close ();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("序列化保存时出错,出错原因为:" + ex.ToString());
                return false;
            }
        }

        public static T Load(string fileName)
        {
            if (File.Exists(fileName) == false)
            {
                return default(T);
            }

            T obj = default(T);
            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(T));
                
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    obj = (T)xml.Deserialize(fs);
                    //fs.Close ();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("序列化读取时出错,出错原因为:" + ex.ToString());
                return default(T);
            }
            return obj;
        }

        public static T Clone(T target)
        {
            T obj = default (T);
            try
            {
                MemoryStream ms = new MemoryStream ();
                XmlSerializer xml = new XmlSerializer (typeof (T));
                xml.Serialize (ms, target);
                ms.Seek (0, SeekOrigin.Begin);
                obj = (T)xml.Deserialize (ms);
            }
            catch (Exception ex)
            {
                MessageBox.Show ("拷贝时出错,出错原因为:" + ex.ToString ());
                return default (T);
            }
            return obj;
        }
    }
}

 

 

 

 

 

例如我们有个简单的类

 class Apply
    {
        [CategoryAttribute("基本参数"), DisplayName("片数")]
        public int WaferNum { get; set; } = 25;
        [CategoryAttribute("基本参数"), DisplayName("文件原路径")]
        public string OriFilePath { get; set; } = "D:\\Data";

    }

 

需要去导入,保存,深拷贝复制,我们就可以这样调用

Apply apply = new Apply();

XMLSerializer<Apply>.Save(apply , "D:\\Appply.xml");



Apply apply = XMLSerializer<Apply>.Load( "D:\\Appply.xml");


//也可以作为类的复制快捷方式来使用--例如下面类这样
class App
        {

            int a = 0;

            public App Clone()
            {
                App a;
                a = XMLSerializer<App>.Clone(this);
                return a;
            }


        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识