- XmlSerializer
- Demonstrating Serialization
- Serialization Attributes
Demonstrating Serialization
The first process that I will illustrate involves exporting an instance of the Domain class to an XML document using the Serialize method of XmlSerializer. Serialize has a variety of method overloads to handle different I/O objects: Stream, TextWriter, and XmlWriter. The easiest way to deal with file I/O is to use the StreamWriter; it provides a TextWriter-compatible interface on top of the file system.
Before we can serialize, we have to create security object hierarchy that provides us with sufficient content for educational value by executing our CreateDomain method. After object creation, we pass the class type, a reference to our top-level Domain object, and the filename as parameters to the ExportDomain method in Listing 2. It relays the parameters to the Serialize method of the XmlSerializer creating the XML document in Listing 3.
Listing 2: Code for Export and Import of Domain Class via XML
using System; using System.IO; using System.Xml.Serialization; namespace Serialize { class SerializeMain { static void Main(string[] args) { // call routine that constructs our // tree of domain objects Domain expdomain = CreateDomain(); ExportDomain(typeof(Domain), expdomain,"C:\\Domain.xml"); // call routine that retrieves a // tree of domain objects from disk // and returns us an object reference Domain impdomain = ImportDomain(typeof(Domain), "C:\\Domain.xml"); DisplayDomain(impdomain); } static string GetNewGUIDString() { // create a GUID return Guid.NewGuid().ToString(); } static void ExportDomain(System.Type type, Object o, string filename) { // create the serializer XmlSerializer serializer = new XmlSerializer(type); // set up the serialize to write its output to // to a streamwriter that is pointing at a file StreamWriter writer = new StreamWriter(filename); // serialize the object to disk serializer.Serialize(writer,o); // close the writer to release the file resource writer.Close(); } static Domain ImportDomain(System.Type type, string filename) { // create the serializer XmlSerializer serializer = new XmlSerializer(type); // create a streamreader that reads from the // input file StreamReader reader = new StreamReader(filename); // instruct serializer to deserialize an object // reference form the file and cast it to the // appropriate reference type Domain newdomain = (Domain) serializer.Deserialize(reader); // close the reader to release the file resource reader.Close(); // return our newly minted object return newdomain; } static Domain CreateDomain() { Domain domain = new Domain(); domain.ID = GetNewGUIDString(); domain.Name = "InformIT"; User[] users = new User[3]; users[0] = new User(); users[0].ID = GetNewGUIDString(); users[0].Name = "Dale Michalk"; users[0].Password = "password"; users[1] = new User(); users[1].ID = GetNewGUIDString(); users[1].Name = "Joe User"; users[1].Password = "password"; users[2] = new User(); users[2].ID = GetNewGUIDString(); users[2].Name = "Guest User"; users[2].Password = "password"; Group[] groups = new Group[3]; groups[0] = new Group(); groups[0].ID = GetNewGUIDString(); groups[0].Name = "Admins"; groups[0].Users = new UserRef[1]; groups[0].Users[0] = new UserRef(); groups[0].Users[0].ID = users[0].ID; groups[1] = new Group(); groups[1].ID = GetNewGUIDString(); groups[1].Name = "Users"; groups[1].Users = new UserRef[2]; groups[1].Users[0] = new UserRef(); groups[1].Users[0].ID = users[0].ID; groups[1].Users[1] = new UserRef(); groups[1].Users[1].ID = users[1].ID; groups[2] = new Group(); groups[2].ID = GetNewGUIDString(); groups[2].Name = "Guests"; groups[2].Users = new UserRef[1]; groups[2].Users[0] = new UserRef(); groups[2].Users[0].ID = users[2].ID; domain.Users = users; domain.Groups = groups; return domain; } static void DisplayDomain(Domain domain) { // Domain Console.WriteLine("Domain: {0}",domain.Name); Console.WriteLine("\tID:{0}",domain.ID); // Users Console.WriteLine(""); Console.WriteLine("Users"); foreach (User user in domain.Users) { Console.WriteLine("\tName:{0}",user.Name); Console.WriteLine("\t\tID:{0}",user.ID); Console.WriteLine("\t\tPassword:{0}", user.Password); } // Groups Console.WriteLine(""); Console.WriteLine("Groups"); foreach (Group group in domain.Groups) { Console.WriteLine("\tName:{0}",group.Name); Console.WriteLine("\t\tID:{0}",group.ID); Console.WriteLine("\t\tUsers:"); foreach (UserRef userref in group.Users) { Console.WriteLine("\t\t\t{0}", ,userref.ID); } Console.WriteLine(""); } } } }
Listing 3: XML Document Result from ExportDomain
<?xml version="1.0" encoding="utf-8"?> <Domain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ID>2ca519b2-a558-4b7b-b243-408588ba629a</ID> <Name>InformIT</Name> <Users> <User> <ID>03ae3f5a-e46d-4fcf-bc33-fdaeee22dcc0</ID> <Name>Dale Michalk</Name> <Password>password</Password> </User> <User> <ID>2831d8af-55aa-4f7c-b212-c87e7fc1eea2</ID> <Name>Joe User</Name> <Password>password</Password> </User> <User> <ID>0c5864a6-b37d-4424-b57e-3a2b86f0e183</ID> <Name>Guest User</Name> <Password>password</Password> </User> </Users> <Groups> <Group> <ID>91d08bbd-8838-4413-8dcf-c63fb02848e0</ID> <Name>Admins</Name> <Users> <UserRef> <ID>03ae3f5a-e46d-4fcf-bc33-fdaeee22dcc0</ID> </UserRef> </Users> </Group> <Group> <ID>b36c1cfb-7de1-4ac9-8f21-9de7ab3cf76d</ID> <Name>Users</Name> <Users> <UserRef> <ID>03ae3f5a-e46d-4fcf-bc33-fdaeee22dcc0</ID> </UserRef> <UserRef> <ID>2831d8af-55aa-4f7c-b212-c87e7fc1eea2</ID> </UserRef> </Users> </Group> <Group> <ID>db3efa55-f228-435c-9833-e62f7dca4078</ID> <Name>Guests</Name> <Users> <UserRef> <ID>0c5864a6-b37d-4424-b57e-3a2b86f0e183</ID> </UserRef> </Users> </Group> </Groups> </Domain>
To rehydrate an object from an existing XML store, we use the DeSerialize method of the XmlSerializer. It is overloaded to take a variety of input sources: Stream, TextReader, or XmlReader. In the ImportDomain method, the example uses the StreamReader to pull the XML document from disk and provide a TextReader-compatible source. To verify the process, the code calls on the DisplayDomain method to spit out information to the console. The data in the document and object instance report in Listing 4 should be the same if the process is working properly.
Listing 4: DisplayDomain Console Output
Domain: InformIT ID:830651e9-b7e7-4c81-9a99-34cc7f583264 Users Name:Dale Michalk ID:99a13f10-9317-41d2-9370-62581371a49f Password:password Name:Joe User ID:3d3cc57d-5a19-491a-be4e-e2bee5e4bdd7 Password:password Name:Guest User ID:2d113809-d77f-427a-b5c9-17641b2a8467 Password:password Groups Name:Admins ID:85317acc-8991-47e7-af0b-3d97bf45aab4 Users: 99a13f10-9317-41d2-9370-62581371a49f Name:Users ID:2bb3ccc7-d386-41cf-b480-c8c1e9860aa2 Users: 99a13f10-9317-41d2-9370-62581371a49f 3d3cc57d-5a19-491a-be4e-e2bee5e4bdd7 Name:Guests ID:d119d679-f4fc-4ba2-b3d2-52f2146965c9 Users: 2d113809-d77f-427a-b5c9-17641b2a8467