Class XmlWriterDecoratorExtensions
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
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.
public static void WriteEncapsulatingElementIfNotNull<T>(this IDecorator<XmlWriter> decorator, T value, XmlQualifiedEntity elementName, Action<XmlWriter, T> nodeWriter)
Parameters
decoratorIDecorator<XmlWriter>The IDecorator{XmlWriter} to extend.
valueTThe object to serialize.
elementNameXmlQualifiedEntityThe optional fully qualified name of the element.
nodeWriterAction<XmlWriter, T>The delegate node writer.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ArgumentNullException
decoratorcannot be null.
WriteObject(IDecorator<XmlWriter>, object, Type, Action<XmlFormatterOptions>)
Serializes the specified value into an XML format of the enclosed XmlWriter of the specified decorator.
public static void WriteObject(this IDecorator<XmlWriter> decorator, object value, Type objectType, Action<XmlFormatterOptions> setup = null)
Parameters
decoratorIDecorator<XmlWriter>The IDecorator{XmlWriter} to extend.
valueobjectThe object to serialize.
objectTypeTypeThe type of the object to serialize.
setupAction<XmlFormatterOptions>The XmlFormatterOptions which may be configured.
Exceptions
- ArgumentNullException
decoratorcannot be null.
WriteObject<T>(IDecorator<XmlWriter>, T, Action<XmlFormatterOptions>)
Serializes the specified value into an XML format of the enclosed XmlWriter of the specified decorator.
public static void WriteObject<T>(this IDecorator<XmlWriter> decorator, T value, Action<XmlFormatterOptions> setup = null)
Parameters
decoratorIDecorator<XmlWriter>The IDecorator{XmlWriter} to extend.
valueTThe object to serialize.
setupAction<XmlFormatterOptions>The XmlFormatterOptions which may be configured.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ArgumentNullException
decoratorcannot be null.
WriteStartElement(IDecorator<XmlWriter>, XmlQualifiedEntity)
Writes the specified start tag and associates it with the given elementName of the enclosed XmlWriter of the specified decorator.
public static void WriteStartElement(this IDecorator<XmlWriter> decorator, XmlQualifiedEntity elementName)
Parameters
decoratorIDecorator<XmlWriter>The IDecorator{XmlWriter} to extend.
elementNameXmlQualifiedEntityThe fully qualified name of the element.
Exceptions
- ArgumentNullException
decoratorcannot be null.
WriteXmlRootElement<T>(IDecorator<XmlWriter>, T, Action<XmlWriter, T, XmlQualifiedEntity>, XmlQualifiedEntity)
Writes the XML root element to the enclosed XmlWriter of the specified decorator.
public static void WriteXmlRootElement<T>(this IDecorator<XmlWriter> decorator, T value, Action<XmlWriter, T, XmlQualifiedEntity> treeWriter, XmlQualifiedEntity rootEntity = null)
Parameters
decoratorIDecorator<XmlWriter>The IDecorator{XmlWriter} to extend.
valueTThe object to serialize.
treeWriterAction<XmlWriter, T, XmlQualifiedEntity>The delegate used to write the XML hierarchy.
rootEntityXmlQualifiedEntityThe optional XmlQualifiedEntity that will provide the name of the root element.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ArgumentNullException
decoratorcannot be null.