StorageServer/IO/XmlReader.cs

30 lines
905 B
C#
Raw Permalink Normal View History

2024-03-24 10:29:56 +09:00
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);
}
}
}