The purpose of this article is to illustrate Deserializing Xml data to object data that resides in application memory. Additionally this article details implementing generics, resulting in a single method being able to deserialize multiple object types.
This article relates to the article “C# How to: Implementing Xml Serialization through a generic extension method“
Sample source code
This article is accompanied by a sample source code Visual Studio project which is available for download here.
Example custom data type
The sample source code provided with this article provides a user defined data type, the CustomDataType class of which the code snippet is listed below.
Also included is sample Xml markup which will be used in deserialization. The included Xml was generated by the serialization code sample “C# How to: Implementing Xml Serialization through a generic extension method“
The deserialization method – Implemented as a String type extension method with generic type support
Extension method architecture enables developers to create methods which, from a syntactic and implementation point of view appear to be part of an existing data type. Extension methods create the perception of being updates or additions, literarily extending a data type as the name implies. Extension methods do not require access to the source code of the particular types being extended, nor does the implementation thereof require recompilation of the referenced types.
This article illustrates a combined implementation of extending the string type whilst specifying the generic type to deserialize to. The following code snippet provides the extension method definition.
The DeserializeXML method satisfies the requirements of the extension method architecture by being defined as a static method, implemented as a member method of a statically defined class. In addition the method signature features the this keyword preceding all other method parameters. The seemingly contradicting statement of specifying the this keyword in a static context usually serves as a quick indication that a method is implemented as an extension method. Remember that the this keyword provides a reference to the current instance, whereas in the case of static methods and classes there is no current instance, being static results in limiting a type to only one instance accessed as a shared reference.
The DeserializeXML method specifies as a parameter xmlString of type string. When defining an extension method the first parameter specified indicates the type being extended. This method’s definition therefore indicates that the extension method extends the string type.
DeserializeXML’s method definition implements a generic type <T>. The calling code will specify the required type substitute for <T>, which in turn will determine the method’s return type.
The DeserializeXML method discussed above will appear as a non-static member method to all objects of type string.
The code snippet listed above illustrates how the DeserializeXML extension discussed earlier now appears as a member method of the string type. Xml data read from the file system is “passed” to the DeserializeXML extension method, specifying <CustomDataType> is to be used as the generic type parameter implemented when performing deserialization.