Admin
5 天以前 bd6818fc9d40f343547bafca0743658f3c0379dc
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
 
namespace WIDESEA.Common
{
    public class XMLSerializationTool
    {
        /// <summary>
        /// 发序列化成对象
        /// </summary>
        /// <param name="type"></param>
        /// <param name="xml"></param>
        /// <returns></returns>
 
        public static T DeserializeXmlToObject<T>(string receiveXml)
        {
            using (StreamReader reader = new StreamReader(
                new MemoryStream(
                    Encoding.UTF8.GetBytes(receiveXml)),
                    Encoding.UTF8))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    T rel = (T)serializer.Deserialize(reader);
                    return rel;
                }
                finally { reader.Close(); }
            }
        }
 
        /// <summary>
        /// 序列化成xml文件格式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObjectToXml<T>(T obj)
        {
            string rel = string.Empty;
 
            using (MemoryStream ms = new MemoryStream())
            {
                try
                {
                    //去掉默认的 xmlns标签
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
 
                    StreamWriter sw = new StreamWriter(ms);
                    //设置UTF-8
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Encoding = Encoding.UTF8;
                    settings.Indent = true;
                    using (XmlWriter writer = XmlWriter.Create(sw, settings))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(T));
                        serializer.Serialize(writer, obj, ns);
                        writer.Flush();
                        writer.Close();
                    }
                    using (StreamReader sr = new StreamReader(ms))
                    {
                        ms.Position = 0;
                        rel = sr.ReadToEnd();
                        sr.Close();
                    }
                }
                finally
                {
                    ms.Close();
                }
            }
 
            ////设置UTF-8
            //XmlWriterSettings xws = new XmlWriterSettings();
            //xws.Encoding = Encoding.UTF8;
            //StringBuilder sb = new StringBuilder();
            //XmlWriter xw = XmlWriter.Create(sb, xws);
 
 
            //XmlSerializer xs = new XmlSerializer(typeof(T));
            //xs.Serialize(xw, obj, ns);
            //xw.Close();
            return rel;
        }
 
 
        //public static string SerializeObjectToXml<T>(T obj)
        //{
        //    string rel = string.Empty;
        //    using (MemoryStream storedStream = new MemoryStream())
        //    {
        //        try
        //        {
        //            //去掉默认的 xmlns标签
        //            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        //            ns.Add("", "");
 
        //            new XmlSerializer(typeof(T)).Serialize(storedStream, obj, ns);
        //            storedStream.Position = 0;
        //            using (StreamReader reader = new StreamReader(storedStream, Encoding.UTF8))
        //            {
        //                try { rel = reader.ReadToEnd(); }
        //                finally { reader.Close(); }
        //            }
        //        }
        //        finally { storedStream.Close(); }
        //    }
        //    return rel;
        //}
    }
}