Class XmlSerializer
- Namespace
- Cuemon.Xml.Serialization
- Assembly
- Cuemon.Xml.dll
Serializes and deserializes objects into and from the XML format.
public class XmlSerializer
- Inheritance
-
XmlSerializer
Examples
The following example demonstrates how to serialize and deserialize a simple object with
using System;
using Cuemon.Xml.Serialization;
namespace MyApp.Examples;
public static class XmlSerializerExample
{
public static void Demonstrate()
{
var serializer = XmlSerializer.Create(new XmlSerializerOptions
{
RootName = new XmlQualifiedEntity("Order")
});
var order = new Order
{
Id = 1001,
Customer = "John Doe",
Total = 299.99m
};
using var stream = serializer.Serialize(order, typeof(Order));
stream.Position = 0;
var deserialized = serializer.Deserialize<Order>(stream);
Console.WriteLine(deserialized.Customer);
Console.WriteLine(deserialized.Total);
}
private sealed class Order
{
public int Id { get; set; }
public string Customer { get; set; }
public decimal Total { get; set; }
}
}
Methods
Create(XmlSerializerOptions)
Creates a new XmlSerializer instance using the specified XmlSerializerOptions.
public static XmlSerializer Create(XmlSerializerOptions settings)
Parameters
settingsXmlSerializerOptionsThe settings to be applied to the XmlSerializer.
Returns
- XmlSerializer
A new XmlSerializer instance using the specified XmlSerializerOptions.
Remarks
If settings is null, DefaultSettings is tried invoked. Otherwise, as a fallback, a default instance of XmlSerializerOptions is created.
Deserialize(Stream, Type)
Deserializes the specified value into an object of objectType.
public object Deserialize(Stream value, Type objectType)
Parameters
valueStreamThe object to deserialize from XML format.
objectTypeTypeThe type of the object to deserialize.
Returns
- object
An object of
objectType.
Deserialize<T>(Stream)
Deserializes the specified value into an object of T.
public T Deserialize<T>(Stream value)
Parameters
valueStreamThe object to deserialize from XML format.
Returns
- T
An object of
T.
Type Parameters
TThe type of the object to deserialize.
Serialize(object, Type)
Serializes the specified value to a Stream.
public Stream Serialize(object value, Type objectType)
Parameters
valueobjectThe object to serialize to XML format.
objectTypeTypeThe type of the object to serialize.
Returns
- Stream
A stream of the serialized object.