-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathXmlSerializationHelper.cs
More file actions
51 lines (40 loc) · 1.35 KB
/
XmlSerializationHelper.cs
File metadata and controls
51 lines (40 loc) · 1.35 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
using System;
using System.Xml.Serialization;
namespace PS5_NOR_Modifier.Common.Helpers
{
public static class XmlSerializationHelper
{
public static T? DeserilazeXmlFromFile<T>(string filePath)
{
T? result = default;
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException("File path is empty");
}
if (!File.Exists(filePath))
{
throw new FileNotFoundException("Local XML file '" + filePath + "' not found.");
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamReader reader = new StreamReader(filePath))
{
result = (T?)serializer.Deserialize(reader);
}
return result;
}
public static T? DeserilazeXmlFromString<T>(string xmlData)
{
T? result = default;
if (string.IsNullOrEmpty(xmlData))
{
throw new ArgumentNullException("XML data to deserialize is empty");
}
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlData))
{
result = (T?)serializer.Deserialize(reader);
}
return result;
}
}
}