static void Main(string[] args) {
BookMark bm1, bm2;
bm1 = new BookMark {
URL = "http://www.amazon.com",
Description = "Amazon.com Web site"
};
bm2 = new BookMark() {
URL = "http://www.wrox.com",
Description = "Wrox.com Web site",
NextURL = null
};
//---link the first BookMark to the next---
bm1.NextURL = bm2;
//========Binary Serialization=========
//---serializing an object graph into a memory stream---
MemoryStream ms = Serialize(bm1);
//---deserializing a memory stream into an object graph---
BookMark bm3 = Deserialize(ms);
//---print out all the bookmarks---
BookMark tempBookMark = bm3;
do {
Console.WriteLine(tempBookMark.URL);
Console.WriteLine(tempBookMark.Description);
Console.WriteLine(tempBookMark.GetDateCreated());
Console.WriteLine(" ");
tempBookMark = tempBookMark.NextURL;
} while (tempBookMark != null);
Console.ReadLine();
}
If the objects are serialized and deserialized correctly, the output is as shown in Figure 11-12.
Figure 11-12
But what does the binary stream look like? To answer that question, take a look at the c:\BookMarks.dat
file that you have created in the process. To view the binary file, simply drag and drop the BookMarks.dat
file into Visual Studio 2008. You should see something similar to Figure 11-13.
Figure 11-13
A few observations are worth noting at this point:
□ Private variables and properties are all serialized. In binary serialization, both the private variables and properties are serialized. This is known as deep serialization, as opposed to shallow serialization in XML serialization (which only serializes the public variables and properties). The next section discusses XML serialization.
□ Object graphs are serialized and preserved. In this example, two BookMark
objects are linked, and the serialization process takes care of the relationships between the two objects.
There are times when you do not want to serialize all of the data in your object. If you don't want to persist the date and time that the BookMark
objects are created, for instance, you can prefix the variable name (that you do not want to serialize) with the [NonSerialized]
attribute:
[Serializable]
class BookMark {
[NonSerialized]
private DateTime _dateCreated;
public BookMark() {
_dateCreated = DateTime.Now;
}
public DateTime GetDateCreated() {
return _dateCreated;
}
public string URL { get; set; }
public string Description { get; set; }
public BookMark NextURL { get; set; }
}
The dateCreated
variable will not be serialized. Figure 11-14 shows that when the dateCreated
variable is not serialized, its value is set to the default date when the object is deserialized.
Figure 11-14
You can also serialize an object into an XML document. There are many advantages to XML serialization. For instance, XML documents are platform-agnostic because they are in plain text format and that makes cross-platform communication quite easy. XML documents are also easy to read and modify, which makes XML a very flexible format for data representation.
The following example illustrates XML serialization and shows you some of its uses.
Defining a Sample Class
Let's define a class so that you can see how XML serialization works. For this example, you define three classes that allow you to store information about a person, such as name, address, and date of birth. Here are the class definitions:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
namespace Serialization {
class Program {
static void Main(string[] args) { }
}
public class Member {
private int age;
public MemberName Name;
public MemberAddress[] Addresses;
public DateTime DOB;
public int currentAge {
get {
//---add a reference to Microsoft.VisualBasic.dll---
age = (int)DateAndTime.DateDiff(
DateInterval.Year, DOB, DateTime.Now,
FirstDayOfWeek.System, FirstWeekOfYear.System);
return age;
}
}
}
public class MemberName {
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class MemberAddress {
public string Line1;
public string Line2;
public string City;
public string Country;
public string Postal;
}
}
The various classes are deliberately designed to illustrate the various aspects of XML serialization. They may not adhere to the best practices for defining classes.
Here are the specifics for the classes:
□ The Member
class contains both private and public members. It also contains a read-only property.
□ The Member
class contains a public array containing the address of a Member.
□ The Member
class contains a variable of Date
data type.
□ The MemberName
class contains two properties.
□ The MemberAddress
class contains only public members.
Serializing the Class
To serialize a Member
object into a XML document, you can use the XMLSerializer
class from the System.Xml.Serialization
namespace:
static void Main(string[] args) {}
//========XML Serialization=========
static void XMLSerialize(Member mem) {
StreamWriter sw = new StreamWriter(@"c:\Members.xml");
Читать дальше