Everything in ExtendedXmlSerializer begins with a configuration container, from which you can use to apply configurations and ultimately create the serializer:
IExtendedXmlSerializer serializer = new ConfigurationContainer()
// Configure...
.Create();
Using this simple subject class:
public sealed class Subject
{
public string Message { get; set; }
public int Count { get; set; }
}
The results of the default serializer will look like this:
<?xml version="1.0" encoding="utf-8"?>
<Subject xmlns="clr-namespace:ExtendedXmlSerializer.Samples.Introduction;assembly=ExtendedXmlSerializer.Samples">
<Message>Hello World!</Message>
<Count>6776</Count>
</Subject>
We can take this a step further by configuring the Subject
's Type
and
Member
properties, which will effect how its Xml is emitted. Here is an
example of configuring the Subject
's element name to emit as
ModifiedSubject
:
IExtendedXmlSerializer serializer = new ConfigurationContainer().ConfigureType<Subject>()
.Name("ModifiedSubject")
.Create();
<?xml version="1.0" encoding="utf-8"?>
<ModifiedSubject xmlns="clr-namespace:ExtendedXmlSerializer.Samples.Introduction;assembly=ExtendedXmlSerializer.Samples">
<Message>Hello World!</Message>
<Count>6776</Count>
</ModifiedSubject>
Diving a bit further, we can also configure the type's member
information. For example, configuring Subject.Message
to emit as Text
instead:
IExtendedXmlSerializer serializer = new ConfigurationContainer().ConfigureType<Subject>()
.Member(x => x.Message)
.Name("Text")
.Create();
<?xml version="1.0" encoding="utf-8"?>
<Subject xmlns="clr-namespace:ExtendedXmlSerializer.Samples.Introduction;assembly=ExtendedXmlSerializer.Samples">
<Text>Hello World!</Text>
<Count>6776</Count>
</Subject>
Xml Settings
In case you want to configure the XML write and read settings via
XmlWriterSettings
and XmlReaderSettings
respectively, you can do that
via extension methods created for you to do so:
Subject subject = new Subject{ Count = 6776, Message = "Hello World!" };
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
string contents = serializer.Serialize(new XmlWriterSettings {Indent = true}, subject);
// ...
And for reading:
Subject instance = serializer.Deserialize<Subject>(new XmlReaderSettings{IgnoreWhitespace = false}, contents);
// ...
Serialization
Now that your configuration container has been configured and your serializer has been created, it's time to get to the serialization.
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
TestClass obj = new TestClass();
string xml = serializer.Serialize(obj);
Deserialization
TestClass obj2 = serializer.Deserialize<TestClass>(xml);
Classic Serialization/Deserialization Patterns
Most of the code examples that you see in this documentation make use of useful extension methods that make serialization and deserialization a snap with ExtendedXmlSerializer. However, if you would like to break down into the basic, classical pattern of serialization, and deserialization, this is supported too, as seen by the following examples. Here's serialization:
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
TestClass instance = new TestClass();
MemoryStream stream = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(stream))
{
serializer.Serialize(writer, instance);
writer.Flush();
}
stream.Seek(0, SeekOrigin.Begin);
string contents = new StreamReader(stream).ReadToEnd();
And here's how to deserialize:
TestClass deserialized;
MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(contents));
using (XmlReader reader = XmlReader.Create(contentStream))
{
deserialized = (TestClass)serializer.Deserialize(reader);
}
w/ Settings Overrides
Additionally, ExtendedXmlSerializer also supports overrides for
serialization and deserialization that allow you to pass in
XmlWriterSettings
and XmlReaderSettings
respectively. Here's an example
of this for serialization:
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
TestClass instance = new TestClass();
MemoryStream stream = new MemoryStream();
string contents = serializer.Serialize(new XmlWriterSettings { /* ... */ }, stream, instance);
And for deserialization:
MemoryStream contentStream = new MemoryStream(Encoding.UTF8.GetBytes(contents));
TestClass deserialized = serializer.Deserialize<TestClass>(new XmlReaderSettings{ /* ... */ }, contentStream);