-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSerializer.cs
More file actions
49 lines (35 loc) · 885 Bytes
/
Copy pathSerializer.cs
File metadata and controls
49 lines (35 loc) · 885 Bytes
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
using System.IO;
using System.Xml.Serialization;
namespace MarvinsAIRA
{
public static class Serializer
{
public static object? Load( string filePath, Type type )
{
var xmlSerializer = new XmlSerializer( type );
using var fileStream = new FileStream( filePath, FileMode.Open );
object? data = null;
try
{
data = xmlSerializer.Deserialize( fileStream );
}
catch ( Exception )
{
}
fileStream.Close();
return data;
}
public static void Save( string filePath, object data )
{
var directoryName = Path.GetDirectoryName( filePath );
if ( directoryName != null )
{
Directory.CreateDirectory( directoryName );
}
var xmlSerializer = new XmlSerializer( data.GetType() );
using var streamWriter = new StreamWriter( filePath );
xmlSerializer.Serialize( streamWriter, data );
streamWriter.Close();
}
}
}