SharpXml - .NET XML serializer written in F#

by Gregor Uhlenheuer on January 13, 2013

SharpXml is an independent, dependency-free and fast .NET XML serialization library. It is written in F# and is built on .NET 4.0.

The project is inspired by the great .NET JSON serializer ServiceStack.Text.

Get it

You can get SharpXml either by installing via nuget, by downloading the precompiled binaries or by cloning the git repository from github and compiling the library on your own.


SharpXml can be found and installed via nuget:

PM> Install-Package SharpXml

Download binaries

You can also download the latest precompiled binaries using the downloads page on github:

Source files

Alternatively you can clone the git repository and compile the project by yourself:

$ git clone git://
$ cd SharpXml\SharpXml
$ msbuild

Lean API

The API tries to appear small and descriptive at the same time:

T can be any .NET POCO type. Apart from others SharpXml supports all basic collection types residing in System.Collections, System.Collections.Generic and System.Collections.Specialized.


SharpXml intends to work in a convention based manner meaning that there won’t be too many configuration options to change its basic (de-)serialization behavior. A few options to modify SharpXml’s output exist anyways:

Custom serialization

Although SharpXml comes with built-in support of all basic .NET types there are two ways to modify its de-/serialization behavior. You can either add custom serialization and/or deserialization logic by registering serialization delegates for a specified type on the static XmlConfig class or you can modify serialization of collections using the XmlElementAttribute in the SharpXml.Common namespace.

Moreover the serialization and deserialization of struct types may be customized by overriding the public ToString() method and/or providing a static ParseXml() function.

Registering delegates

The XmlElementAttribute in SharpXml.Common allows you to modify the default serialization of .NET types using a few properties to choose from:

XML format

In the following section I want to give a short description of the format SharpXml generates and expects on deserialization.

The first thing to mention is that public properties are serialized and deserialized only. Fields whether public or not are not serialized at the moment and won’t be in the future! Attributes placed inside the XML tags are not supported either and are simply ignored. Apart from that serialization is pretty straight-forward and your XML looks like you would probably expect it anyway – at least from my point of view :-)

Basic serialization

An instance of the class above will be serialized like the following:

Using XmlConfig.EmitCamelCaseNames = true; the generated XML output would look like this instead:


SharpXml will generate the following XML:

Key-value collections (dictionaries)

The serialized output by SharpXml looks like the following:

Note: In all XML examples above indentation is added for convenience only.

Using XmlElementAttribute

As mentioned before you can use the XmlElementAttribute to customize the generated XML output which is especially useful for collection and dictionary types.

This example shows the effect of the four major options given by the XmlElementAttribute: Name, ItemName, KeyName and ValueName.

Root type namespaces

Using the property Namespace of the XmlElementAttribute you can set an optional namespace string that will be used on serialization of the root element of the resulting XML document:

The class described above will be serialized like the following:

Struct types

Non-reference types like struct may provide custom implementation of the methods ToString() and/or ParseXml() in order to customize SharpXml’s serialization behavior.

A typical example might look like this:

Using the struct type described above results in the following output:

Without the custom implementations the struct would be serialized like this:

Custom serialization delegates

Moreover reference types can be customized by registering custom serialization delegates to the static XmlConfig class using the aforementioned RegisterSerializer and RegisterDeserializer functions.

The resulting XML will look pretty much the same as the struct example described earlier but you can imagine the possibilities given by this approach.


The deserialization logic of SharpXml can be described as very fault-tolerant meaning that usually bad formatted or even invalid XML may be deserialized without errors.

In order to provide a better view on how fault-tolerant SharpXml works I will give an example of a very bad formatted XML input that will be deserialized without any errors:

This XML above will be successfully deserialized into an instance of MyClass.


Some random things I am planning to work on in the future:


SharpXml is written by Gregor Uhlenheuer. You can reach me at


SharpXml is licensed under the Apache license, Version 2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This post is tagged with .net, programming, xml, f# and serialization