- First Update

This commit is contained in:
2022-03-07 14:35:38 +09:00
parent 1ab1cfd4ea
commit f53695b228
48 changed files with 17003 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
using System;
using System.IO;
using System.Xml.Serialization;
namespace CommonLibrary
{
public static class XMLSystem
{
public static bool Save<T>(string path, T data) where T : struct
{
if (string.IsNullOrWhiteSpace(path))
{
return false;
}
string directory = Path.GetDirectoryName(System.IO.Path.GetFullPath(path));
if (!System.IO.Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
using (StreamWriter sw = new StreamWriter(path))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
xmlSerializerNamespaces.Add("", "");
xmlSerializer.Serialize(sw, data, xmlSerializerNamespaces);
sw.Close();
}
return true;
}
public static T LoadFromPath<T>(string path) where T : struct
{
if (string.IsNullOrWhiteSpace(path))
{
return default;
}
string directory = Path.GetDirectoryName(System.IO.Path.GetFullPath(path));
if (!System.IO.Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(path))
{
return default;
}
using (StreamReader sr = new StreamReader(path))
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
T data = (T)xmlSerializer.Deserialize(sr);
return data;
}
catch (Exception ex)
{
CommonLibrary.Log.ERROR(ex.Message);
return default;
}
}
}
public static T LoadFromData<T>(string data) where T : struct
{
if (string.IsNullOrWhiteSpace(data))
{
return default;
}
using (StringReader sr = new StringReader(data))
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(sr);
}
catch (Exception ex)
{
CommonLibrary.Log.ERROR(ex.Message);
return default;
}
}
}
}
}