无名

长风破浪会有时,直挂云帆济沧海!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace SerializableTest
{
    
class Programaa
    {
       
        
public void SerializeDocument()
        {
            
string filename = "e:\\books.xml";
            
// Creates a new XmlSerializer.
            XmlSerializer s =
            
new XmlSerializer(typeof(MyRootClass));

            
// Writing the file requires a StreamWriter.
            TextWriter myWriter = new StreamWriter(filename);

            
// Creates an instance of the class to serialize. 
            MyRootClass myRootClass = new MyRootClass();

            
/* Uses a more advanced method of creating an list:
         create instances of the Item and BookItem, where BookItem 
         is derived from Item. 
*/
            Item item1 
= new Item();
            
// Sets the objects' properties.
            item1.ItemName = "Widget1";
            item1.ItemCode 
= "w1";
            item1.ItemPrice 
= 231;
            item1.ItemQuantity 
= 3;

            BookItem bookItem 
= new BookItem();
            
// Sets the objects' properties.
            bookItem.ItemCode = "w2";
            bookItem.ItemPrice 
= 123;
            bookItem.ItemQuantity 
= 7;
            bookItem.ISBN 
= "34982333";
            bookItem.Title 
= "Book of Widgets";
            bookItem.Author 
= "John Smith";

            
// Sets the class's Items property to the list.
            myRootClass.Items.Add(item1);
            myRootClass.Items.Add(bookItem);

            
/* Serializes the class, writes it to disk, and closes 
               the TextWriter. 
*/
            s.Serialize(myWriter, myRootClass);
            myWriter.Close();
        }

        
public MyRootClass DeSerialize()
        {
            TextReader reader 
= new StreamReader("e:\\books.xml");
            XmlSerializer serializer 
=
           
new XmlSerializer(typeof(MyRootClass));
            var myBooks 
= (MyRootClass)serializer.Deserialize(reader);
            reader.Close();
            
return myBooks;
        }

    }

    
// This is the class that will be serialized.
    [Serializable]
    
public class MyRootClass
    {
        
public MyRootClass()
        {
            items 
= new List<Item>();
        }

        
private List<Item> items;

        [XmlArrayItem(ElementName 
= "Item",
   IsNullable 
= true,
   Type 
= typeof(Item)),
   XmlArrayItem(ElementName 
= "BookItem",
   IsNullable 
= true,
   Type 
= typeof(BookItem))]

        
public List<Item> Items
        {
            
get { return items; }
            
set { items = value; }
        }
    }

    
public class Item
    {
        [XmlElement(ElementName 
= "OrderItem")]
        
public string ItemName;
        
public string ItemCode;
        
public decimal ItemPrice;
        
public int ItemQuantity;
    }

    
public class BookItem : Item
    {
        
public string Title;
        
public string Author;
        
public string ISBN;
    }



}

 

http://tech.ddvip.com/2010-03/1268192963146672_3.html

http://msdn.microsoft.com/en-us/library/ms950721.aspx

 

posted on 2010-04-23 00:56  无名  阅读(236)  评论(0编辑  收藏  举报