Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 21-18 extracted from chapter XML


Listing 21-17<     > Listing 21-19


This listing can be compiled with the command line:
csc.exe /target:exe Example_21_18.cs
Errors: 0 Warnings: 0


Example_21_18.cs
using System.Xml;
using System.Xml.Serialization;
using System.IO;
public class book {
   public string genre  { getreturn m_genre; set{ m_genre = value; } }
   private string m_genre;
   public string title  { getreturn m_title; set{ m_title = value; } }
   private string m_title;
   public decimal price { getreturn 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) xmls.Deserialize(fs2);
      fs2.Close();
   }
}	
Copyright Patrick Smacchia 2006 2007