|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
ContentsIntroductionThis C# program demonstrates loading and saving an object containing a bitmap and collections to an XML file using .NET XML serialization. XML serialization enables an object's public fields and properties to be saved and loaded to/from an XML file. This article focuses on the C# 2.0 (Visual Studio 2005) implementation. The C# 1.1 code does not use Generics and static class declaration, because those features are only available in C# 2.0. BackgroundAfter discovering the usefulness of XML in storing data (including configuration settings for a program), I needed a solution to load and save an object's public fields and properties to/from an XML file without the complexity of navigating through the DOM (Document Object Model). I also wanted to provide an abstraction of the XML data as class objects, and only concern myself with the XML specifics when saving or loading data to/from a file. In addition, this 'load/save to XML' framework had to be reusable in other projects. A practical solution - and the one demonstrated here - is to use the functionality provided by the .NET Using the codeFile descriptions
Enabling XML serialization of an objectBefore we can serialize an object to XML, the object's class code must include various custom metadata attributes (e.g., // Set this 'Customer' class as the root node
// of any XML file its serialized to.
[XmlRootAttribute("Customer", Namespace="", IsNullable=false)]
public class Customer
{
private Bitmap picture;
/// <summary>
/// Default constructor for this class
/// (required for serialization).
/// </summary>
public Customer()
{
}
// Set this 'DateTimeValue' field
// to be an attribute of the root node.
[XmlAttributeAttribute(DataType="date")]
public System.DateTime DateTimeValue;
// By NOT specifing any custom
// Metadata Attributes, fields will be created
// as an element by default.
public int CustomerID;
public string CustomerName;
public int Age;
// Set serialization to IGNORE
// this field (ie. not add it to the XML).
[XmlIgnoreAttribute()]
public bool CustomerPaid;
// Set serialization to IGNORE this property
// because the 'PictureByteArray' property
// is used instead to serialize
// the 'Picture' Bitmap as an array of bytes.
[XmlIgnoreAttribute()]
public Bitmap Picture
{
get { return picture; }
set { picture = value; }
}
// Serializes the 'Picture' Bitmap to XML.
[XmlElementAttribute("Picture")]
public byte[] PictureByteArray
{
get
{
if (picture != null)
{
TypeConverter BitmapConverter =
TypeDescriptor.GetConverter(picture.GetType());
return (byte[])
BitmapConverter.ConvertTo(picture, typeof(byte[]));
}
else
return null;
}
set
{
if (value != null)
picture = new Bitmap(new MemoryStream(value));
else
picture = null;
}
}
// Serializes an ArrayList as a "Hobbies" array
// of XML elements of type string named "Hobby".
[XmlArray ("Hobbies"), XmlArrayItem("Hobby", typeof(string))]
public System.Collections.ArrayList Hobbies =
new System.Collections.ArrayList();
// Serializes an ArrayList as a "EmailAddresses" array
// of XML elements of custom type EmailAddress named "EmailAddress".
[XmlArray ("EmailAddresses"),
XmlArrayItem("EmailAddress", typeof(EmailAddress))]
public System.Collections.ArrayList EmailAddresses =
new System.Collections.ArrayList();
}
Class to encapsulate XML serializationThe The The Methods with the The following private methods within private static T LoadFromBinaryFormat(string path,
IsolatedStorageFile isolatedStorageFolder)
{
T serializableObject = null;
using (FileStream fileStream =
CreateFileStream(isolatedStorageFolder, path))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
serializableObject = binaryFormatter.Deserialize(fileStream) as T;
}
return serializableObject;
}
private static T LoadFromDocumentFormat(System.Type[] extraTypes,
string path, IsolatedStorageFile isolatedStorageFolder)
{
T serializableObject = null;
using (TextReader textReader =
CreateTextReader(isolatedStorageFolder, path))
{
XmlSerializer xmlSerializer = CreateXmlSerializer(extraTypes);
serializableObject = xmlSerializer.Deserialize(textReader) as T;
}
return serializableObject;
}
private static void SaveToBinaryFormat(T serializableObject,
string path, IsolatedStorageFile isolatedStorageFolder)
{
using (FileStream fileStream =
CreateFileStream(isolatedStorageFolder, path))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, serializableObject);
}
}
private static void SaveToDocumentFormat(T serializableObject,
System.Type[] extraTypes, string path,
IsolatedStorageFile isolatedStorageFolder)
{
using (TextWriter textWriter =
CreateTextWriter(isolatedStorageFolder, path))
{
XmlSerializer xmlSerializer = CreateXmlSerializer(extraTypes);
xmlSerializer.Serialize(textWriter, serializableObject);
}
}
private static FileStream CreateFileStream(IsolatedStorageFile
isolatedStorageFolder, string path)
{
FileStream fileStream = null;
if (isolatedStorageFolder == null)
fileStream = new FileStream(path, FileMode.OpenOrCreate);
else
fileStream = new IsolatedStorageFileStream(path,
FileMode.OpenOrCreate, isolatedStorageFolder);
return fileStream;
}
private static TextReader CreateTextReader(IsolatedStorageFile
isolatedStorageFolder, string path)
{
TextReader textReader = null;
if (isolatedStorageFolder == null)
textReader = new StreamReader(path);
else
textReader = new StreamReader(new IsolatedStorageFileStream(path,
FileMode.Open, isolatedStorageFolder));
return textReader;
}
private static TextWriter CreateTextWriter(IsolatedStorageFile
isolatedStorageFolder, string path)
{
TextWriter textWriter = null;
if (isolatedStorageFolder == null)
textWriter = new StreamWriter(path);
else
textWriter = new StreamWriter(new IsolatedStorageFileStream(path,
FileMode.OpenOrCreate, isolatedStorageFolder));
return textWriter;
}
private static XmlSerializer CreateXmlSerializer(System.Type[] extraTypes)
{
Type ObjectType = typeof(T);
XmlSerializer xmlSerializer = null;
if (extraTypes != null)
xmlSerializer = new XmlSerializer(ObjectType, extraTypes);
else
xmlSerializer = new XmlSerializer(ObjectType);
return xmlSerializer;
}
Saving an object to an XML fileThe following example code shows how to use the // Create customer object based on Form values.
Customer customer = this.CreateCustomer();
//Save customer object to XML file using our ObjectXMLSerializer class...
ObjectXMLSerializer<Customer>.Save(customer, XML_FILE_NAME);
Loading an object from an XML fileThe following example code shows how to use the // Load the customer object from the XML file using our custom class...
customer = ObjectXMLSerializer<Customer>.Load(XML_FILE_NAME);
Points of interestStoring a bitmap in an XML fileThe Conversion from a Note that two public properties are used to enable storing of the Storing a collection of strings in an XML fileThe [XmlArray ("Hobbies"), XmlArrayItem("Hobby", typeof(string))]
The XML attribute, <Hobbies>
<Hobby>Golf</Hobby>
<Hobby>Tennis</Hobby>
<Hobby>Reading</Hobby>
</Hobbies>
Storing a collection of custom objects in an XML fileThe Immediately before the class declaration, the XML attribute A default constructor with no parameters is included in the The XML attribute <EmailAddresses>
<EmailAddress Address="joebloggs@zenicom.com" Destination="Business" />
<EmailAddress Address="joebloggs@athome.com" Destination="Home" />
<EmailAddress Address="joebloggs@hotmail.com" Destination="Other" />
</EmailAddresses>
References
HistoryVersion 1.0Original submission. Version 2.0
Version 2.1
Version 3.0
Version 3.1
| ||||||||||||||||||||