Table of Contents

XmlWriterDecoratorExtensions Class

Definition

Namespace
Cuemon.Xml
Assemblies
Cuemon.Xml.dll
Source
src/Cuemon.Xml/Extensions/XmlWriterDecoratorExtensions.cs

Extension methods for the XmlWriter class hidden behind the IDecorator<T> interface.

public static class XmlWriterDecoratorExtensions
Inheritance
XmlWriterDecoratorExtensions

Examples

XmlWriterDecoratorExtensions provides extension methods on Decorator.Enclose for serializing objects directly to XmlWriter with root element handling, conditional encapsulation, and custom root naming. This example creates an XmlWriter targeting a StringWriter with indented settings, then uses WriteXmlRootElement with a custom delegate that writes a root element, serializes an anonymous Person object, and conditionally wraps notes in an encapsulating <Notes> element via WriteEncapsulatingElementIfNotNull. A second example shows WriteObject to serialize a Version with a custom RootName = new XmlQualifiedEntity("Version"). Console output displays both XML results with the correct element structure.

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

namespace MyApp.Examples;

public class Example
{
    public void Run()
    {

        var writer = new StringWriter();
        var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = false });

        var decorator = Decorator.Enclose(xmlWriter);

        // Write a root element and serialize an object
        var person = new { FirstName = "John", LastName = "Doe", Age = 30 };
        decorator.WriteXmlRootElement(person, (w, value, rootEntity) =>
        {
            decorator.WriteStartElement(rootEntity);                          // <Root>
            decorator.WriteObject(value, typeof(object));                      // serialized person content
            decorator.WriteEncapsulatingElementIfNotNull("notes", new XmlQualifiedEntity("Notes"), (w2, notes) =>
            {
                w2.WriteString(notes);                                        // <Notes>notes</Notes>
            });
        });

        xmlWriter.Flush();
        string xml = writer.ToString();
        Console.WriteLine(xml);

        // Write an object directly without root element handling
        var version = new Version(1, 0, 0, 0);
        var xmlWriter2 = XmlWriter.Create(new StringWriter(), new XmlWriterSettings { Indent = true });
        var decorator2 = Decorator.Enclose(xmlWriter2);
        decorator2.WriteObject(version, o =>
        {
            o.Settings.RootName = new XmlQualifiedEntity("Version");
        });
        xmlWriter2.Flush();
        // <Version>
        //   <Major>1</Major>
        //   <Minor>0</Minor>
        //   <Build>0</Build>
        //   <Revision>0</Revision>
        // </Version>

}
}

Methods

Name Description
WriteEncapsulatingElementIfNotNull<T>(IDecorator<XmlWriter>, T, XmlQualifiedEntity, Action<XmlWriter, T>)

Writes the specified value with the delegate nodeWriter to the enclosed XmlWriter of the specified decorator. If elementName is not null, then the delegate nodeWriter is called from within an encapsulating Start- and End-element.

WriteObject(IDecorator<XmlWriter>, object, Type, Action<XmlFormatterOptions>)

Serializes the specified value into an XML format of the enclosed XmlWriter of the specified decorator.

WriteObject<T>(IDecorator<XmlWriter>, T, Action<XmlFormatterOptions>)

Serializes the specified value into an XML format of the enclosed XmlWriter of the specified decorator.

WriteStartElement(IDecorator<XmlWriter>, XmlQualifiedEntity)

Writes the specified start tag and associates it with the given elementName of the enclosed XmlWriter of the specified decorator.

WriteXmlRootElement<T>(IDecorator<XmlWriter>, T, Action<XmlWriter, T, XmlQualifiedEntity>, XmlQualifiedEntity)

Writes the XML root element to the enclosed XmlWriter of the specified decorator.

See Also