30 lines
905 B
C#
30 lines
905 B
C#
|
using System.Xml.Serialization;
|
||
|
|
||
|
namespace StorageServer.IO;
|
||
|
|
||
|
public static class XmlReader {
|
||
|
public static void Save<T>(string filePath, T obj) {
|
||
|
try {
|
||
|
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||
|
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
|
||
|
serializer.Serialize(fs, obj);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
Console.WriteLine($"Error: {e.Message}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static T Read<T>(string filePath) {
|
||
|
try {
|
||
|
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||
|
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
|
||
|
return (T) serializer.Deserialize(fs);
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
Console.WriteLine($"Error: {e.Message}");
|
||
|
return default(T);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|