XML Serialization, part 2

As I promised, I made an example of using serialization and deserialization in C#

First of all we have to create class of objects which will be serialized/deserialized, let’s say Person.cs:


namespace Sample
{
 using System.Xml.Serialization;

/// <summary>
 /// The person.
 /// </summary>
 public class Person
 {
 /// <summary>
 /// Gets or sets the its name.
 /// </summary>
 [XmlElement("Name")]
 public string itsName { get; set; }

/// <summary>
 /// Gets or sets the its surname.
 /// </summary>
 [XmlElement("Surname")]
 public string itsSurname { get; set; }

/// <summary>
 /// Gets or sets the its age.
 /// </summary>
 [XmlElement("Age")]
 public int itsAge { get; set; }
 }
}

Then we should write a sample program which would do this 2 operations:


namespace Sample
{
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Xml.Serialization;

/// <summary>
 /// The program.
 /// </summary>
 public class Program
 {
 /// <summary>
 /// The main.
 /// </summary>
 /// <param name="args">
 /// The args.
 /// </param>
 public static void Main(string[] args)
 {
 var objectsToSerialize = new List<Person>
 {
 new Person() { itsName = "Jack", itsSurname = "Jackson", itsAge = 20 },
 new Person() { itsName = "Bob", itsSurname = "Bobess", itsAge = 21 }
 };

SerializeObjectToXml(objectsToSerialize);

objectsToSerialize.Clear();

 objectsToSerialize = DeserializeObjectFromXml(@"c:\test\test.xml");
foreach (var person in objectsToSerialize)
 {
 Console.Write(person.itsName + " " + person.itsSurname + " " + person.itsAge + Environment.NewLine);
 }

Console.ReadKey();
 }

/// <summary>
 /// The serialize object to xml.
 /// </summary>
 /// <param name="listOfPeople">
 /// The list of people.
 /// </param>
 private static void SerializeObjectToXml(List<Person> listOfPeople)
 {
 var newSerializer = new XmlSerializer(typeof(List<Person>));
 using (TextWriter swriter = new StreamWriter(@"c:\test\test.xml"))
 {
 newSerializer.Serialize(swriter, listOfPeople);
 }
 }

/// <summary>
 /// The deserialize object from xml.
 /// </summary>
 /// <param name="pathToXml">
 /// The path to xml.
 /// </param>
 /// <returns>
 /// The <see cref="List"></see> .
 /// </returns>
 private static List<Person> DeserializeObjectFromXml(string pathToXml)
 {
 var newDeserializer = new XmlSerializer(typeof(List<Person>));
 List<Person> newListForPeople;
 using (TextReader sreader = new StreamReader(pathToXml))
 {
 newListForPeople = (List<Person>)newDeserializer.Deserialize(sreader);
 }

return newListForPeople;
 }
 }
}

As you can see, first we serialize list of objects, and then clear this list. After this 2 operations we are able to find xml file at specified path. It should look like this:


<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Person>
 <Name>Jack</Name>
 <Surname>Jackson</Surname>
 <Age>20</Age>
 </Person>
 <Person>
 <Name>Bob</Name>
 <Surname>Bobess</Surname>
 <Age>21</Age>
 </Person>
</ArrayOfPerson>

The last operation is to deserialize objects saved in this xml file. To do it we should initialize XmlSerializer object and then, using TextReader, read all of the values and insert them into a list which was cleared before this. That’s all!

Jedna uwaga do wpisu “XML Serialization, part 2

Zostaw komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Connecting to %s