xml 序列化

ExpandedBlockStart.gif 代码
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

 

转载于:https://www.cnblogs.com/wuming/archive/2010/04/23/1718242.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值