RSCG – StackXML
| name | StackXML |
| nuget | https://www.nuget.org/packages/StackXML/ |
| link | https://github.com/ZingBallyhoo/StackXML |
| author | Zing |
Generating XML serializer without reflection
This is how you can use StackXML .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="StackXML" Version="1.0.0" OutputItemType="Analyzer" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
The code that you will use is
using SerializerDemo;
using StackXML;
var p= new Person() { Name= "Andrei Ignat" , Age=55};
var str= XmlWriteBuffer.SerializeStatic(p);
Console.WriteLine(str);
var entity = XmlReadBuffer.ReadStatic<Person>(str);
Console.WriteLine("name is "+entity.Name);
using StackXML;
namespace SerializerDemo;
[XmlCls("person")]
public partial class Person
{
[XmlField("age")]
public int Age;
[XmlField("name")]
public string Name = string.Empty;
}
The code that is generated is
using System;
using System.IO;
using System.Collections.Generic;
using StackXML;
using StackXML.Str;
// <auto-generated/>
#pragma warning disable
namespace SerializerDemo
{
/// <inheritdoc cref="Person"/>
partial class Person : IXmlSerializable
{
public virtual ReadOnlySpan<char> GetNodeName()
{
return "person";
}
public virtual bool ParseAttribute(ref XmlReadBuffer buffer, ReadOnlySpan<char> name, ReadOnlySpan<char> value)
{
switch (name)
{
case "age": {
this.Age = buffer.m_params.m_stringParser.Parse<System.Int32>(value);
return true;
}
case "name": {
this.Name = value.ToString();
return true;
}
}
return false;
}
public virtual void SerializeAttributes(ref XmlWriteBuffer buffer)
{
buffer.PutAttribute("age", Age);
buffer.PutAttribute("name", Name);
}
public virtual bool ParseFullBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> bodySpan, ref int end)
{
return false;
}
public virtual bool ParseSubBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> name, ReadOnlySpan<char> bodySpan, ReadOnlySpan<char> innerBodySpan, ref int end, ref int endInner)
{
return false;
}
public virtual void SerializeBody(ref XmlWriteBuffer buffer)
{
}
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/StackXML