Table of Contents

Class XmlWriterExtensions

Namespace
Cuemon.Extensions.Xml
Assembly
Cuemon.Extensions.Xml.dll

Extension methods for the XmlWriter class.

public static class XmlWriterExtensions
Inheritance
XmlWriterExtensions

Examples

XmlWriterExtensions provides extension methods for XmlWriter including object serialization, qualified element names, conditional wrapping, and root element generation. This example creates an XmlWriter targeting a StringWriter with indented, declaration-free settings, then calls WriteObject to serialize an InvalidOperationException, WriteStartElement with an XmlQualifiedEntity("Cuemon") for a standalone element, WriteEncapsulatingElementWhenNotNull to conditionally wrap an exception in a "MyWrappedElement", and WriteXmlRootElement to produce a Root element with a "cuemon" namespace URI. Console output displays four separate XML results showing the serialized exception, the standalone element, the wrapped exception, and the namespace-qualified root element.

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

namespace DocExamples;

public static class XmlWriterExtensionsExample
{
    public static void Demonstrate()
    {
        Console.WriteLine(WriteException());
        Console.WriteLine(WriteStandaloneElement());
        Console.WriteLine(WriteWrappedException());
        Console.WriteLine(WriteRootElement());
    }

    private static string WriteException()
    {
        var output = new StringWriter();
        using var writer = XmlWriter.Create(output, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
        writer.WriteObject(new InvalidOperationException());
        writer.Flush();
        return output.ToString();
    }

    private static string WriteStandaloneElement()
    {
        var output = new StringWriter();
        using var writer = XmlWriter.Create(output, new XmlWriterSettings { OmitXmlDeclaration = true });
        writer.WriteStartElement(new XmlQualifiedEntity("Cuemon"));
        writer.WriteEndElement();
        writer.Flush();
        return output.ToString();
    }

    private static string WriteWrappedException()
    {
        var output = new StringWriter();
        using var writer = XmlWriter.Create(output, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
        writer.WriteEncapsulatingElementWhenNotNull(new InvalidOperationException(), new XmlQualifiedEntity("MyWrappedElement"), (nestedWriter, exception) =>
        {
            nestedWriter.WriteObject(exception);
        });
        writer.Flush();
        return output.ToString();
    }

    private static string WriteRootElement()
    {
        var output = new StringWriter();
        using var writer = XmlWriter.Create(output, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
        writer.WriteXmlRootElement(new InvalidOperationException(), (nestedWriter, exception, rootEntity) =>
        {
            nestedWriter.WriteObject(exception);
        }, new XmlQualifiedEntity("Root", "cuemon"));
        writer.Flush();
        return output.ToString();
    }
}

Methods

WriteEncapsulatingElementWhenNotNull<T>(XmlWriter, T, XmlQualifiedEntity, Action<XmlWriter, T>)

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

public static void WriteEncapsulatingElementWhenNotNull<T>(this XmlWriter writer, T value, XmlQualifiedEntity elementName, Action<XmlWriter, T> nodeWriter)

Parameters

writer XmlWriter

The XmlWriter to extend.

value T

The object to serialize.

elementName XmlQualifiedEntity

The optional fully qualified name of the element.

nodeWriter Action<XmlWriter, T>

The delegate node writer.

Type Parameters

T

The type of the object to serialize.

Exceptions

ArgumentNullException

writer cannot be null.

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

Serializes the specified value into an XML format.

public static void WriteObject(this XmlWriter writer, object value, Type objectType, Action<XmlFormatterOptions> setup = null)

Parameters

writer XmlWriter

The XmlWriter to extend.

value object

The object to serialize.

objectType Type

The type of the object to serialize.

setup Action<XmlFormatterOptions>

The XmlFormatterOptions which may be configured.

Exceptions

ArgumentNullException

writer cannot be null.

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

Serializes the specified value into an XML format.

public static void WriteObject<T>(this XmlWriter writer, T value, Action<XmlFormatterOptions> setup = null)

Parameters

writer XmlWriter

The XmlWriter to extend.

value T

The object to serialize.

setup Action<XmlFormatterOptions>

The XmlFormatterOptions which may be configured.

Type Parameters

T

The type of the object to serialize.

Exceptions

ArgumentNullException

writer cannot be null.

WriteStartElement(XmlWriter, XmlQualifiedEntity)

Writes the specified start tag and associates it with the given elementName.

public static void WriteStartElement(this XmlWriter writer, XmlQualifiedEntity elementName)

Parameters

writer XmlWriter

The XmlWriter to extend.

elementName XmlQualifiedEntity

The fully qualified name of the element.

Exceptions

ArgumentNullException

writer cannot be null.

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

Writes the XML root element to an existing writer.

public static void WriteXmlRootElement<T>(this XmlWriter writer, T value, Action<XmlWriter, T, XmlQualifiedEntity> treeWriter, XmlQualifiedEntity rootEntity = null)

Parameters

writer XmlWriter

The XmlWriter to extend.

value T

The object to serialize.

treeWriter Action<XmlWriter, T, XmlQualifiedEntity>

The delegate used to write the XML hierarchy.

rootEntity XmlQualifiedEntity

The optional XmlQualifiedEntity that will provide the name of the root element.

Type Parameters

T

The type of the object to serialize.

Exceptions

ArgumentNullException

writer cannot be null.