|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionDuring the development process of our project, the need arose to serialize objects to XML. For this purpose, the .NET provides the First, the creation of the class is expensive in performance. This is caused by the fact the class creates an in-memory assembly to quickly read and write from the objects to serialize. It does this by using reflection to inspect the object. This leads to the second disadvantage; only public properties and fields are serialized. These properties also have to be writable so that the AlternativesTo get around these problems, you can use the XmlFormatterThe code accompanied by this article contains our
The following example shows the use of the formatter, by serializing a class called using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace XmlFormatterSample
{
[Serializable]
public class MyObject : ISerializable
{
public string MyPublicProperty
{
get { return _myPublicProperty; }
set { _myPublicProperty = value; }
}
private string _myPublicProperty;
private string MyPrivateProperty
{
get { return _myPrivateProperty; }
set { _myPrivateProperty = value; }
}
private string _myPrivateProperty;
public MyObject()
{
this._myPublicProperty = "This is my public property";
this._myPrivateProperty = "This is my private property";
}
public string GetProperties()
{
string properties = "MyPublicProperty = " +
MyPublicProperty + "\r\n";
properties += "MyPrivateProperty = " +
MyPrivateProperty;
return properties;
}
#region ISerializable Members
/// <summary>
/// Special serialization constructor.
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public MyObject(SerializationInfo info,
StreamingContext context)
{
_myPublicProperty = info.GetString("MyPublicProperty");
_myPrivateProperty = info.GetString("MyPrivateProperty");
}
/// <summary>
/// Interface method to place the properties
/// in the serialization queue
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("MyPublicProperty", MyPublicProperty);
info.AddValue("MyPrivateProperty", MyPrivateProperty);
}
#endregion
}
}
The following code serializes and deserializes the object from and to a using DotNetMagazine.September.Examples;
using System.IO;
using System.Xml;
using System.Data.SqlTypes;
namespace XmlFormatterSample
{
class Program
{
static void Main(string[] args)
{
MyObject object1 = new MyObject();
MyObject object2;
// write the properties to the console
Console.WriteLine("The properties of object1 are:");
Console.WriteLine(object1.GetProperties());
Console.WriteLine();
// serialize the object
// ***************************************
MemoryStream stream = new MemoryStream();
XmlFormatter serializer =
new XmlFormatter(typeof(MyObject));
serializer.Serialize(stream, object1);
// reset the stream to the beginning
stream.Position = 0;
SqlXml xml = new SqlXml(stream);
// ***************************************
// write the XML value to the console
Console.WriteLine("Xml value of object 1:");
Console.WriteLine(xml.Value);
Console.WriteLine();
// recreate the object in object 2
using (MemoryStream stream2 =
new MemoryStream(Encoding.UTF8.GetBytes(xml.Value)))
{
object2 = (MyObject)serializer.Deserialize(stream2);
}
// write the properties to the console
Console.WriteLine("The properties of object2 are:");
Console.WriteLine(object2.GetProperties());
Console.WriteLine();
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
}
}
The code is free to use in your projects. This code is also part of an article published in the Dutch version of the .NET magazine, September issue 2006: "Objecten en Sql Server 2005, XML als intermediair". We hope this formatter will be of use to you!
|
||||||||||||||||||||||||||||||