chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
 
namespace UtilLib
{
    public static class XmlHelper
    {
 
        static readonly XmlSerializerNamespaces DEFAULT_NS = new XmlSerializerNamespaces();
 
        static XmlHelper()
        {
            DEFAULT_NS.Add("", "");
        }
 
        /// <summary>
        /// serialize object to xml file.
        /// </summary>
        /// <param name="path">the path to save the xml file</param>
        /// <param name="obj">the object you want to serialize</param>
        public static void SerializeToXml(string path, object obj)
        {
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            string content = string.Empty;
            //serialize
            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, obj, DEFAULT_NS);
                content = writer.ToString();
            }
            //save to file
            using (StreamWriter stream_writer = new StreamWriter(path))
            {
                stream_writer.Write(content);
            }
        }
 
        /// <summary>
        /// xml 文件反序列化为对象
        /// </summary>
        /// <typeparam name="T">序列化类型</typeparam>
        /// <param name="path">the path of the xml file</param>
        /// <returns>对象</returns>
        public static T DeserializeFromXml<T>(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StreamReader reader = new StreamReader(path))
            {
                return (T)serializer.Deserialize(reader);
            }
        }
        public static bool ReadFromXml<T>(T obj, string path)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(path);
 
            Type type = typeof(T);
            XmlNode root = xd.SelectSingleNode(type.Name);
            if (root == null)
            {
                return false;
            }
 
            //获取 RefHero 中所有的实例私有字段
            FieldInfo[] fieldArray = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
            foreach (FieldInfo filed in fieldArray)
            {
                string fileName = filed.Name;
                XmlNode node = root.SelectSingleNode(fileName);
                if (node == null)
                {
                    continue;
                }
                filed.SetValue(obj, node.InnerText);
            }
            return true;
        }
 
        public static T DeepClone<T>(T data)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                serializer.Serialize(stream, data);
                stream.Seek(0, SeekOrigin.Begin);
                return (T)serializer.Deserialize(stream);
            }
        }
    }
}