Skip to content

thomasgalliker/XmlSerializerHelper

Repository files navigation

XmlSerializerHelper

Version Downloads Buy Me a Coffee

XmlSerializerHelper

XmlSerializerHelper serializes and deserializes any .NET object from/to XML. It internally uses XmlSerializer which is part of the System.Xml.Serialization namespace. On top of XmlSerializer it adds a feature to preserve original type information.

Download and Install XmlSerializerHelper

This library is available on NuGet: https://www.nuget.org/packages/XmlSerializerHelper Use the following command to install XmlSerializerHelper using NuGet package manager console:

PM> Install-Package XmlSerializerHelper

You can use this library in any .NET Standard or .NET project.

API Usage

Serializing a .NET object to XML

The following unit test shows how a simple .NET object of type SimpleSerializerClass is serialized to XML:

// Arrange
IXmlSerializerHelper xmlSerializerHelper = new XmlSerializerHelper();
var obj = new SimpleSerializerClass { BoolProperty = true, StringProperty = "test" };
Type targetType = obj.GetType();

// Act
string serializedString = xmlSerializerHelper.SerializeToXml(obj);

// Assert
serializedString.Should().NotBeNullOrEmpty();

Deserialize an XML string to a .NET object

The following unit test shows the above serializedString can be deserialized again:

// Arrange
// ...

// Act
var deserializedObject = (SimpleSerializerClass)xmlSerializerHelper.DeserializeFromXml(targetType, serializedString);

// Assert
deserializedObject.Should().NotBeNull();
deserializedObject.BoolProperty.Should().BeTrue();
deserializedObject.StringProperty.Should().Be("test");

Validate the XSD schema for an XML file

XsdValidator can be used to validate XML content against a given XSD schema.

// Arrange
string xmlContent = XmlTestData.GetValidXmlContent();
string xsdContent = XmlTestData.GetXsdMarkup();

IXsdValidator xsdValidator = new XsdValidator();

// Act
var validationResult = xsdValidator.Validate(xmlContent, xsdContent);

// Assert
validationResult.IsValid.Should().BeTrue();

Using object extension methods

There is a number of extension methods for type System.Object in ObjectExtensions. You can simply call SerializeToXml resp. DeserializeFromXml on any .NET object. Following unit test gives an example:

// Arrange
float inputValue = 123.456f;

// Act
var serializedString = inputValue.SerializeToXml();

// Assert
serializedString.Should().NotBeNullOrEmpty();

Dependency management

If you do not use an IoC framework, you can call XmlSerializerHelper.Current to get a singleton instance of IXmlSerializerHelper.

string serializedString = XmlSerializerHelper.Current.SerializeToXml(value: new object(), preserveTypeInformation: false);

The same applies to XsdValidator.Current. However, it is advised to register the mentioned interfaces along with their respective implementations in an IoC framework.

License

This project is Copyright © 2026 Thomas Galliker.

About

Serializes and deserializes any .Net object from/to XML

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages