移动系统liao
2024-07-30 35a617123c0d578772d9f4450bf27d3b52277384
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
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 2021/7/29 23:21:06
 *        Description: 暂无
 ***********************************************************************/
 
 
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.Linq;
using System.Xml.Serialization;
 
namespace CoreCms.Net.WeChat.Service.Utilities
{
    /// <summary>XML 工具类</summary>
    public static class XmlUtility
    {
        /// <summary>反序列化</summary>
        /// <param name="xml">XML字符串</param>
        /// <returns></returns>
        public static object Deserialize<T>(string xml)
        {
            try
            {
                using (StringReader stringReader = new StringReader(xml))
                    return new XmlSerializer(typeof(T)).Deserialize((TextReader)stringReader);
            }
            catch (Exception ex)
            {
                Console.WriteLine((object)ex);
                return (object)null;
            }
        }
 
        /// <summary>反序列化</summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object Deserialize<T>(Stream stream) => new XmlSerializer(typeof(T)).Deserialize(stream);
 
        /// <summary>
        /// 序列化
        /// 说明:此方法序列化复杂类,如果没有声明XmlInclude等特性,可能会引发“使用 XmlInclude 或 SoapInclude 特性静态指定非已知的类型。”的错误。
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string Serializer<T>(T obj)
        {
            MemoryStream memoryStream = new MemoryStream();
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            try
            {
                xmlSerializer.Serialize((Stream)memoryStream, (object)obj);
            }
            catch (InvalidOperationException)
            {
                throw;
            }
            memoryStream.Position = 0L;
            StreamReader streamReader = new StreamReader((Stream)memoryStream);
            string end = streamReader.ReadToEnd();
            streamReader.Dispose();
            memoryStream.Dispose();
            return end;
        }
 
        /// <summary>序列化将流转成XML字符串</summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static XDocument Convert(Stream stream)
        {
            if (stream.CanSeek)
                stream.Seek(0L, SeekOrigin.Begin);
            using (XmlReader reader = XmlReader.Create(stream))
                return XDocument.Load(reader);
        }
 
        /// <summary>序列化将流转成XML字符串</summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string ConvertToString(Stream stream)
        {
            StreamReader reader = new StreamReader(stream);
            string sHtml = reader.ReadToEnd();
            return sHtml;
        }
 
    }
}