Table of Contents

XmlFormatter Class

Definition

Serializes and deserializes an object in XML format.

public class XmlFormatter : StreamFormatter<XmlFormatterOptions>, IConfigurable<XmlFormatterOptions>
Inheritance
object
XmlFormatter
Implements
Inherited Members

Examples

The following example demonstrates how to serialize an object with .

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

namespace MyApp.Examples;

public static class XmlFormatterExample
{
    public static void Demonstrate()
    {
        var formatter = new XmlFormatter();
        var person = new Person { Name = "John Doe", Age = 42 };

        using var stream = formatter.Serialize(person, typeof(Person));
        stream.Position = 0;

        using var reader = new StreamReader(stream);
        var xml = reader.ReadToEnd();

        Console.WriteLine(xml.Contains("John Doe"));
        Console.WriteLine(xml.Contains("42"));
    }

    private sealed class Person
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }
}

Constructors

Name Description
XmlFormatter()

Initializes a new instance of the XmlFormatter class.

XmlFormatter(XmlFormatterOptions)

Initializes a new instance of the XmlFormatter class.

XmlFormatter(Action<XmlFormatterOptions>)

Initializes a new instance of the XmlFormatter class.

Methods

Name Description
Deserialize(Stream, Type)

Deserializes the specified value into an object of objectType.

Serialize(object, Type)

Serializes the specified source to an object of Stream.

SerializeToWriter(XmlWriter, object, Type)

Serializes the specified source into an XML format.

See Also