diff --git a/Lib.Xml/Interfaces/IXmlReader.cs b/Lib.Xml/Interfaces/IXmlReader.cs new file mode 100644 index 0000000..920d2b5 --- /dev/null +++ b/Lib.Xml/Interfaces/IXmlReader.cs @@ -0,0 +1,11 @@ +// Author: Gockner, Simon +// Created: 2021-04-27 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +namespace Lib.Xml.Interfaces +{ + public interface IXmlReader where T : new() + { + T Read(string filePath); + } +} \ No newline at end of file diff --git a/Lib.Xml/Interfaces/IXmlWriter.cs b/Lib.Xml/Interfaces/IXmlWriter.cs new file mode 100644 index 0000000..f12b3fa --- /dev/null +++ b/Lib.Xml/Interfaces/IXmlWriter.cs @@ -0,0 +1,16 @@ +// Author: Gockner, Simon +// Created: 2021-04-27 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +namespace Lib.Xml.Interfaces +{ + public interface IXmlWriter + { + void Write(T data, string filePath); + } + + public interface IXmlWriter where TXml : TData + { + void Write(TData data, string filePath); + } +} \ No newline at end of file diff --git a/Lib.Xml/Lib.Xml.csproj b/Lib.Xml/Lib.Xml.csproj new file mode 100644 index 0000000..d9bcfcd --- /dev/null +++ b/Lib.Xml/Lib.Xml.csproj @@ -0,0 +1,8 @@ + + + + net5.0 + enable + + + diff --git a/Lib.Xml/XmlReader.cs b/Lib.Xml/XmlReader.cs new file mode 100644 index 0000000..aedf690 --- /dev/null +++ b/Lib.Xml/XmlReader.cs @@ -0,0 +1,27 @@ +// Author: Gockner, Simon +// Created: 2021-04-27 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System.IO; +using System.Xml; +using System.Xml.Serialization; +using Lib.Xml.Interfaces; + +namespace Lib.Xml +{ + public class XmlReader : IXmlReader where T : new() + { + public T Read(string filePath) + { + if (!File.Exists(filePath)) + return new T(); + + FileStream file = File.OpenRead(filePath); + + XmlReader xmlReader = XmlReader.Create(file); + XmlSerializer xmlSerializer = new(typeof(T)); + + return (T?) xmlSerializer.Deserialize(xmlReader) ?? new T(); + } + } +} \ No newline at end of file diff --git a/Lib.Xml/XmlWriter.cs b/Lib.Xml/XmlWriter.cs new file mode 100644 index 0000000..6745ecd --- /dev/null +++ b/Lib.Xml/XmlWriter.cs @@ -0,0 +1,32 @@ +// Author: Gockner, Simon +// Created: 2021-04-27 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System.IO; +using System.Xml; +using System.Xml.Serialization; +using Lib.Xml.Interfaces; + +namespace Lib.Xml +{ + public class XmlWriter : XmlWriter, IXmlWriter + { + + } + + public class XmlWriter : IXmlWriter where TXml : TData + { + public void Write(TData data, string filePath) + { + FileStream file = File.OpenWrite(filePath); + + XmlWriter xmlWriter = XmlWriter.Create(file, new XmlWriterSettings {Indent = true}); + XmlSerializer xmlSerializer = new(typeof(TXml)); + + XmlSerializerNamespaces xmlSerializerNamespaces = new(); + xmlSerializerNamespaces.Add("", ""); + + xmlSerializer.Serialize(xmlWriter, data, xmlSerializerNamespaces); + } + } +} \ No newline at end of file diff --git a/Mystify.sln b/Mystify.sln index fedde72..38e93dd 100644 --- a/Mystify.sln +++ b/Mystify.sln @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib.ProcessManaging", "Lib. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib.Tools.Avalonia", "Lib.Tools.Avalonia\Lib.Tools.Avalonia.csproj", "{8F61D9C4-E7C7-440C-AE8C-D55FDA06BC5A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lib.Xml", "Lib.Xml\Lib.Xml.csproj", "{B1F68301-9374-4203-9053-E80E94A0FDCD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -60,5 +62,9 @@ Global {8F61D9C4-E7C7-440C-AE8C-D55FDA06BC5A}.Debug|Any CPU.Build.0 = Debug|Any CPU {8F61D9C4-E7C7-440C-AE8C-D55FDA06BC5A}.Release|Any CPU.ActiveCfg = Release|Any CPU {8F61D9C4-E7C7-440C-AE8C-D55FDA06BC5A}.Release|Any CPU.Build.0 = Release|Any CPU + {B1F68301-9374-4203-9053-E80E94A0FDCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B1F68301-9374-4203-9053-E80E94A0FDCD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B1F68301-9374-4203-9053-E80E94A0FDCD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B1F68301-9374-4203-9053-E80E94A0FDCD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal