Class DefaultXmlConverter
- Namespace
- Cuemon.Xml.Serialization.Converters
- Assembly
- Cuemon.Xml.dll
Provides a default way to convert objects to and from XML.
public sealed class DefaultXmlConverter : XmlConverter
- Inheritance
-
DefaultXmlConverter
- Inherited Members
Examples
The following example demonstrates how DefaultXmlConverter can serialize and deserialize a simple XML value.
using System;
using System.Collections.Generic;
using System.IO;
using Cuemon.Xml.Serialization;
using Cuemon.Xml.Serialization.Converters;
using System.Xml;
namespace MyApp.Examples;
public static class DefaultXmlConverterExample
{
public static void Demonstrate()
{
var converter = new DefaultXmlConverter(new XmlQualifiedEntity("String"), new List<XmlConverter>());
using var buffer = new MemoryStream();
using (var writer = XmlWriter.Create(buffer, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
converter.WriteXml(writer, "Hello World");
}
buffer.Position = 0;
using var reader = XmlReader.Create(buffer);
var value = (string)converter.ReadXml(reader, typeof(string));
Console.WriteLine(value);
Console.WriteLine(converter.CanConvert(typeof(string)));
}
}
Constructors
DefaultXmlConverter(XmlQualifiedEntity, IList<XmlConverter>)
Initializes a new instance of the DefaultXmlConverter class.
public DefaultXmlConverter(XmlQualifiedEntity rootName, IList<XmlConverter> converters)
Parameters
rootNameXmlQualifiedEntityconvertersIList<XmlConverter>
Methods
CanConvert(Type)
Determines whether this instance can convert the specified object type.
public override bool CanConvert(Type objectType)
Parameters
Returns
- bool
trueif this instance can convert the specified object type; otherwise,false.
ReadXml(XmlReader, Type)
Generates an object from its XML representation.
public override object ReadXml(XmlReader reader, Type objectType)
Parameters
readerXmlReaderThe XmlReader stream from which the object is deserialized.
objectTypeTypeThe Type of the object to generate.
Returns
- object
The generated (deserialized) object.
WriteXml(XmlWriter, object, XmlQualifiedEntity)
Converts an object into its XML representation.
public override void WriteXml(XmlWriter writer, object value, XmlQualifiedEntity elementName = null)
Parameters
writerXmlWriterThe XmlWriter stream to which the object is serialized.
valueobjectThe object to convert.
elementNameXmlQualifiedEntityThe element name to encapsulate around
value.
Exceptions
- InvalidOperationException
There is an error in the XML document.