|
Listing 21-20 extracted from chapter XML
Listing 21-19< > Listing 21-21
This listing can be compiled with the command line: csc.exe /target:exe Example_21_20.cs Errors: 1 Warnings: 0
Example_21_20.cs
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xml.Serialization.GeneratedAssembly;
[XmlRoot(ElementName = "livre")]
public class book {
[XmlAttribute(AttributeName = "genre")]
public string genre { get{ return m_genre; } set{ m_genre = value; } }
private string m_genre;
[XmlElement(ElementName = "titre")]
public string title { get{ return m_title; } set{ m_title = value; } }
private string m_title;
[XmlIgnore]
public decimal price { get{ return m_price; } set{ m_price = value; } }
private decimal m_price;
}
public class Program {
static public void Main() {
book b1 = new book();
b1.genre = "autobiography";
b1.title = "The Autobiography of Benjamin Franklin";
b1.price = 8.99M;
// Store the state of an instance of the 'book' class ...
// ... in the 'book.xml' file.
FileStream fs1 = File.OpenWrite("book.xml");
XmlSerializer xmls = new XmlSerializer(typeof(book));
xmls.Serialize(fs1, b1);
fs1.Close();
// Create an instance of the 'book' class ...
// ... from the 'book.xml' file.
FileStream fs2 = File.OpenRead("book.xml");
book b2 = (book)bookS.Deserialize(fs2);
fs2.Close();
}
}
Copyright Patrick Smacchia 2006 2007
|