Introduction
In previous post on topic Working with XML using C#, we saw few basic examples of XML and how to create, load and query XML documents.
Today, we will take one step further and learn, how to persist .NET objects to XML and then we will see example of how to construct back .NET objects from XML.
Storing .NET Object(s) to XML and also Constructing .NET Object(s) from XML are common requirements in most business application. This process is generally called Serialization/De-Serialization.
If you are new XML and .NET topic, I will suggest you to first read the previous post, for background information.
All the source-code, you will see in this post, can be downloaded from this git repository. This is the same code base from previous post, here I will just add new functionality on top of it.
.NET Object
To start with, here I have created a very simple .NET Class. I am using Device entity, but it could be any other object e.g. Product, Customer, Invoice or any other entity.
Device Class
Next, lets see how to serialize this to XML.
Serializing .NET Object to XML
XMLSerializer is a standard way in .NET to serialize objects to XML.
I’ve created a method, which uses XMLSerializer and also use StringWriter to write device object to a string.
Here you can see in the code that the resulting string from this method is the XML as expected:
Saving XML to File
Once, we have the XML string, we can write it to file, save it to database or post it to some external system. For our demo purposes, we will save it to a file:
I am just using File API in .NET to write it to disk and If we now, open the file in a text editor, we can see that XML is written to the file:
Deserializing .NET Object from XML
So .NET to XML was covered in previous section. Now, what if we want to build .NET objects from XML stored in a file (or any other storage). We can easily do that with XMLSerializer as shown in the code below:
As you can see that we are using FileStream to read xml from a file. However, later we will see an example, which uses memory-stream instead of FileStream for the same purpose.
Here is the De-Serialize Method in action:
So, we saw that using XMLSerializer is very straight-forward when we want to serialize/de-serialize our .NET Objects to and from XML.
Lets see next, that how can we simplify this common serialization/deserialization requirement by use of generics and .NET extension methods.
Extension Methods for Simplification
Instead of writing serialize/de-serialize methods for every .NET Class that we want to basically serialize and/or de-serialize, we can create extension methods for simplification and code reusability.
Following is the code stucture for these helper methods:
These extension methods are using generics which results in overall code reduction and increase in simplification. We will fill these methods bodies with code a little bit later. First, lets see how we will be using these methods:
Usage
As you can see in the following code, that we created a device object and then simply called extension methods on the object and because the functionality is wrapped in extension methods, this serialization/de-serialization is available for all the types in our application.
Lets see the actual implementation code for these extension methods:
Serialize Method
Here is the implementation of serialize method. It is actually almost identical to the code, we wrote in the start of this post. I actually copied that code here and adjusted the parameters accordingly.
The method is using XMLSerializer with generic types and then we are using StringWriter and returning XML string as a result.
De-Serialize Method
The de-serialize method is also similar to the code which we saw earlier. However, this time instead of FileStream, this method is using MemoryStream and then returning the object to the caller. Code is self explanatory and that is all we need for this method.
Test Result
Here is the result of running the code and you can see that serializing and de-serialization process is working as expected:
Summary
There are many approaches to work with XML in .NET. XMLSeralizer is a standard way when working with .NET Objects and XML.
We saw examples of how to serialize and de-serialize .NET Objects and XML strings and then wrapped this functionality in extension methods which uses generics and this way we have reduced the amount of code and simplify usage.
You can download the source-code from this git repository.
Let me know if you have some questions or comments. Till next time, Happy Coding.
My Recent Books
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.