Table of Contents

DynamicXmlConverter Class

Definition

Provides a factory based way to create and wrap an XmlConverter implementation.

public static class DynamicXmlConverter
Inheritance
DynamicXmlConverter

Examples

The following example creates a DynamicXmlConverter<int> with custom read and write delegates. Writing the value 42 produces an XML fragment containing a <Value> element; the converter can also read it back from XML.

using System;
using System.IO;
using System.Xml;
using Cuemon.Xml.Serialization;

namespace Cuemon.Xml.Serialization;

public class DynamicXmlConverterExample
{
    public void Demonstrate()
    {
        var converter = DynamicXmlConverter.Create<int>(
            writer: (w, value, entity) =>
            {
                w.WriteElementString("Value", value.ToString());
            },
            reader: (r, type) =>
            {
                r.ReadToDescendant("Value");
                return int.Parse(r.ReadElementContentAsString());
            });

        using var sw = new StringWriter();
        using var writer = XmlWriter.Create(sw);
        converter.WriteXml(writer, 42);
        writer.Flush();
        Console.WriteLine(sw.ToString());
    }
}

Methods

Name Description
Create(Type, Action<XmlWriter, object, XmlQualifiedEntity>, Func<XmlReader, Type, object>, Func<Type, bool>, XmlQualifiedEntity)

Creates a dynamic instance of an XmlConverter implementation wrapping WriteXml(XmlWriter, object, XmlQualifiedEntity) through writer and ReadXml(XmlReader, Type) through reader.

Create<T>(Action<XmlWriter, T, XmlQualifiedEntity>, Func<XmlReader, Type, T>, Func<Type, bool>, XmlQualifiedEntity)

Creates a dynamic instance of an XmlConverter implementation wrapping WriteXml(XmlWriter, object, XmlQualifiedEntity) through writer and ReadXml(XmlReader, Type) through reader.