2023-02-16 09:53:02 +01:00
|
|
|
|
using System.Text;
|
2023-02-09 16:19:14 +01:00
|
|
|
|
using ChoETL;
|
2023-02-09 10:25:42 +01:00
|
|
|
|
using Throw;
|
|
|
|
|
|
2023-02-09 16:19:14 +01:00
|
|
|
|
namespace Application.Common.Extensions;
|
2023-02-09 10:25:42 +01:00
|
|
|
|
|
|
|
|
|
public static class XmlSerializationExtensions
|
|
|
|
|
{
|
|
|
|
|
public static T DeserializeCardData<T>(this string source) where T : class
|
|
|
|
|
{
|
|
|
|
|
using var reader = new ChoXmlReader<T>(new StringReader(source)).WithXPath("/root/data");
|
2023-09-30 10:33:21 +02:00
|
|
|
|
reader.Configuration.IgnoreFieldValueMode = ChoIgnoreFieldValueMode.Any;
|
2023-02-09 10:25:42 +01:00
|
|
|
|
|
|
|
|
|
var result = reader.Read();
|
|
|
|
|
result.ThrowIfNull();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-02-09 16:19:14 +01:00
|
|
|
|
|
|
|
|
|
public static string SerializeCardData<T>(this T source, string xpath) where T : class
|
|
|
|
|
{
|
|
|
|
|
var buffer = new StringBuilder();
|
2023-02-12 18:46:08 +01:00
|
|
|
|
using (var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization())
|
|
|
|
|
{
|
|
|
|
|
writer.Configuration.OmitXmlDeclaration = false;
|
|
|
|
|
writer.Configuration.DoNotEmitXmlNamespace = true;
|
|
|
|
|
writer.Write(source);
|
|
|
|
|
}
|
2023-02-09 16:19:14 +01:00
|
|
|
|
return buffer.ToString();
|
|
|
|
|
}
|
2023-02-14 19:14:19 +01:00
|
|
|
|
|
|
|
|
|
public static string SerializeCardData<T>(this T source) where T : class
|
|
|
|
|
{
|
|
|
|
|
var buffer = new StringBuilder();
|
|
|
|
|
using (var writer = new ChoXmlWriter<T>(buffer).UseXmlSerialization())
|
|
|
|
|
{
|
|
|
|
|
writer.Configuration.OmitXmlDeclaration = false;
|
|
|
|
|
writer.Configuration.DoNotEmitXmlNamespace = true;
|
|
|
|
|
writer.Configuration.IgnoreRootName = true;
|
|
|
|
|
writer.Write(source);
|
|
|
|
|
}
|
|
|
|
|
return buffer.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:19:14 +01:00
|
|
|
|
public static string SerializeCardDataList<T>(this IEnumerable<T> source, string xpath) where T : class
|
|
|
|
|
{
|
|
|
|
|
var buffer = new StringBuilder();
|
2023-02-12 18:46:08 +01:00
|
|
|
|
using (var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization())
|
|
|
|
|
{
|
|
|
|
|
writer.Configuration.OmitXmlDeclaration = false;
|
|
|
|
|
writer.Configuration.DoNotEmitXmlNamespace = true;
|
|
|
|
|
writer.Write(source);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:19:14 +01:00
|
|
|
|
return buffer.ToString();
|
|
|
|
|
}
|
2023-02-09 10:25:42 +01:00
|
|
|
|
}
|