- add lib.xml

master
Simon G 5 years ago
parent ed0ce0497f
commit 0fbd1d419f
  1. 11
      Lib.Xml/Interfaces/IXmlReader.cs
  2. 16
      Lib.Xml/Interfaces/IXmlWriter.cs
  3. 8
      Lib.Xml/Lib.Xml.csproj
  4. 27
      Lib.Xml/XmlReader.cs
  5. 32
      Lib.Xml/XmlWriter.cs
  6. 6
      Mystify.sln

@ -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<out T> where T : new()
{
T Read(string filePath);
}
}

@ -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<in T>
{
void Write(T data, string filePath);
}
public interface IXmlWriter<in TData, in TXml> where TXml : TData
{
void Write(TData data, string filePath);
}
}

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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<T> : IXmlReader<T> 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();
}
}
}

@ -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<T> : XmlWriter<T, T>, IXmlWriter<T>
{
}
public class XmlWriter<TData, TXml> : IXmlWriter<TData, TXml> 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);
}
}
}

@ -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

Loading…
Cancel
Save