} finally {

  xr.Close();

 }

 return obj;

}

Here, you can use the XmlReader class's Create() method to open an XML file for reading.

The XmlReader class is used to read the data from the XML file. The deserialized object is then returned to the calling function.

Remember to import the System.Xml namespace for the XmlReader class.

To test the XMLDeserialize() function, call it directly after an object has been serialized, like this:

static void Main(string[] args) {

 MemberAddress address1 = new MemberAddress() {

  Line1 = 'One Way Street',

  Line2 = 'Infinite Loop',

  Country = 'SINGAPORE',

  Postal = '456123'

 };

 MemberAddress address2 = new MemberAddress() {

  Line1 = 'Two Way Street',

  Country = 'SINGAPORE',

  Postal = '456123'

 };

 Member m1 = new Member() {

  Name = new MemberName() {

  FirstName = 'Wei-Meng',

  LastName = 'Lee'

 },

 DOB = Convert.ToDateTime(@'5/1/1972'),

 Addresses = new MemberAddress[] { address1, address2 }

};

XMLSerialize(m1);

 Member m2 = XMLDeserialize(@'c:Members.xml');

 Console.WriteLine('{0}, {1}', m2.Name.FirstName, m2.Name.LastName);

 Console.WriteLine('{0}', m2.currentAge);

 foreach (MemberAddress a in m2.Addresses) {

  Console.WriteLine('{0}', a.Line1);

  Console.WriteLine('{0}', a.Line2);

  Console.WriteLine('{0}', a.Country);

  Console.WriteLine('{0}', a.Postal);

  Console.WriteLine();

 }

 Console.ReadLine();

}

The output of these statements is shown in Figure 11-16.

Figure 11-16

Customizing the Serialization Process

Despite the fairly automated task performed by the XMLSerializer object, you can customize the way the XML document is generated. Here's an example of how you can modify classes with a few attributes:

[XmlRoot('MemberInformation',

 Namespace = 'http://www.learn2develop.net',

 IsNullable = true)]

public class Member {

 private int age;

  //---specify the element name to be MemberName---

 [XmlElement('MemberName')]

 public MemberName Name;

 //---specify the sub-element(s) of Addresses to be Address---

 [XmlArrayItem('Address')]

 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;

 //---empty element if city is not specified---

 [XmlElement(IsNullable = true)]

 public string City;

 //---specify country and postal as attribute---

 [XmlAttributeAttribute()]

 public string Country;

 [XmlAttributeAttribute()]

 public string Postal;

}

When the class is serialized again, the XML document will look like Figure 11-17.

Figure 11-17

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату