-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlLib.cs
More file actions
162 lines (151 loc) · 5.64 KB
/
XmlLib.cs
File metadata and controls
162 lines (151 loc) · 5.64 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace XmlLibrary
{
public static class XmlLib
{
/// <summary>
/// Serializes the specified object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sObject">The s object.</param>
/// <returns></returns>
/// =====================================================================================
/// Modification : Initial : 07/02/2019 | N.WILCKE(SESA474351)
/// =====================================================================================
public static string Serialize<T>(T sObject)
{
try
{
XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
var xml = string.Empty;
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww))
{
xsSubmit.Serialize(writer, sObject);
xml = sww.ToString();
}
}
return xml;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Converts xml to specified file name in specified fodler.
/// </summary>
/// <param name="xml">The XML.</param>
/// <param name="path">The path.</param>
/// <param name="fileName">Name of the file.</param>
/// =====================================================================================
/// Modification : Initial : 07/02/2019 | N.WILCKE(SESA474351)
/// =====================================================================================
[Obsolete("This method is no longer maintained. Prefer to use SerializeToFile")]
public static void ToFile(string xml, string path, string fileName)
{
try
{
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.Save(path + fileName);
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Serializes to file.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sObject">The s object.</param>
/// <param name="path">The path.</param>
/// <param name="fileName">Name of the file.</param>
/// =====================================================================================
/// Modification : Initial : 07/02/2019 | N.WILCKE(SESA474351)
/// =====================================================================================
public static void SerializeToFile<T>(T sObject, string path, string fileName)
{
try
{
#region Serialize
XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
var xml = string.Empty;
using (var sww = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sww))
{
xsSubmit.Serialize(writer, sObject);
xml = sww.ToString();
}
}
#endregion
#region Save to file
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xml);
xdoc.Save(path + fileName);
#endregion
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Deserializes xml from file to specified object type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">The path.</param>
/// <param name="fileName">Name of the file.</param>
/// <returns></returns>
/// =====================================================================================
/// Modification : Initial : 07/02/2019 | N.WILCKE(SESA474351)
/// =====================================================================================
public static T DeserializeFromFile<T>(string path, string fileName)
{
try
{
var serializer = new XmlSerializer(typeof(T));
using (var file = File.OpenText(path + fileName))
{
var data = (T)serializer.Deserialize(file);
return data;
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Deserializes the specified XML to object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="xml">The XML.</param>
/// <returns></returns>
/// =====================================================================================
/// Modification : Initial : 07/02/2019 | N.WILCKE(SESA474351)
/// =====================================================================================
public static T Deserialize<T>(string xml)
{
try
{
var serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xml))
{
var data = (T)serializer.Deserialize(reader);
return data;
}
}
catch (Exception)
{
throw;
}
}
}
}