1
0
mirror of synced 2024-12-11 22:16:00 +01:00
GC-local-server-rewrite/Application/Common/Extensions/XmlSerializationExtensions.cs

57 lines
1.8 KiB
C#

using System.Linq.Expressions;
using System.Text;
using ChoETL;
using Throw;
namespace Application.Common.Extensions;
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");
var result = reader.Read();
result.ThrowIfNull();
return result;
}
public static string SerializeCardData<T>(this T source, string xpath) where T : class
{
var buffer = new StringBuilder();
using (var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization())
{
writer.Configuration.OmitXmlDeclaration = false;
writer.Configuration.DoNotEmitXmlNamespace = true;
writer.Write(source);
}
return buffer.ToString();
}
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();
}
public static string SerializeCardDataList<T>(this IEnumerable<T> source, string xpath) where T : class
{
var buffer = new StringBuilder();
using (var writer = new ChoXmlWriter<T>(buffer).WithXPath(xpath).UseXmlSerialization())
{
writer.Configuration.OmitXmlDeclaration = false;
writer.Configuration.DoNotEmitXmlNamespace = true;
writer.Write(source);
}
return buffer.ToString();
}
}