C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

object 序列化

using  System;
using  System.Xml;
using  System.Xml.Serialization;
using  System.IO;


[XmlRoot(
" productCatalog " )]
public   class  ProductCatalog 
{

    [XmlElement(
" catalogName " )]
    
public   string  CatalogName;
    
    
//  Use the date data type (and ignore the time portion in the 
    
//  serialized XML).
    [XmlElement(ElementName = " expiryDate " , DataType = " date " )]
    
public  DateTime ExpiryDate;
    
    
//  Configure the name of the tag that holds all products,
    
//  and the name of the product tag itself.
    [XmlArray( " products " )]
    [XmlArrayItem(
" product " )]
    
public  Product[] Products;

    
public  ProductCatalog() 
    
{
        
//  Default constructor for deserialization.
    }


    
public  ProductCatalog( string  catalogName, DateTime expiryDate) 
    
{
        
this .CatalogName  =  catalogName;
        
this .ExpiryDate  =  expiryDate;
    }

}


public   class  Product 
{

    [XmlElement(
" productName " )]
    
public   string  ProductName;
    
    [XmlElement(
" productPrice " )]
    
public   decimal  ProductPrice;
    
    [XmlElement(
" inStock " )]
    
public   bool  InStock;
    
    [XmlAttributeAttribute(AttributeName
= " id " , DataType = " integer " )]
    
public   string  Id;

    
public  Product() 
    
{
        
//  Default constructor for serialization.
    }


    
public  Product( string  productName,  decimal  productPrice) 
    
{
        
this .ProductName  =  productName;
        
this .ProductPrice  =  productPrice;
    }

}

public   class  SerializeXml 
{

    
private   static   void  Main() 
    
{

        
//  Create the product catalog.
        ProductCatalog catalog  =   new  ProductCatalog( " New Catalog " ,
            DateTime.Now.AddYears(
1 ));
        Product[] products 
=   new  Product[ 2 ];
        products[
0 =   new  Product( " Product 1 " 42.99m );
        products[
1 =   new  Product( " Product 2 " 202.99m );
        catalog.Products 
=  products;

        
//  Serialize the order to a file.
        XmlSerializer serializer  =   new  XmlSerializer( typeof (ProductCatalog));
        FileStream fs 
=   new  FileStream( " ProductCatalog.xml " , FileMode.Create);
        serializer.Serialize(fs, catalog);
        fs.Close();

        catalog 
=   null ;

        
//  Deserialize the order from the file.
        fs  =   new  FileStream( " ProductCatalog.xml " , FileMode.Open);
        catalog 
=  (ProductCatalog)serializer.Deserialize(fs);

        
//  Serialize the order to the Console window.
        serializer.Serialize(Console.Out, catalog);
        Console.ReadLine();
    }

}



// store the object in a binary format 

[Serializable] 

public   class  Item 



     
private   string  itemName ; 

     
private   string  itemId; 

     
private   double  itemPrice ; 

     
private   long  itemQuantity; 

     
private   double  netAmount; 

     
public  Item() 

     


            ItemName 
=   & quot; & quot;; 

            ItemPrice 
=   0.00

            ItemQuantity 
=   0

            netAmount 
=   0.00

     }
 

     
public   string  ItemName 

     


            
get  

            


                 
return  itemName; 

            }
 

            
set  

            


                 itemName 
=  value; 

            }
 

     }
 

     
public   string  ItemId 

     


            
get  

            


                 
return  itemId; 

            }
 

            
set  

            


                 itemId 
=  value; 

            }
 

     }
 

     
public   double  ItemPrice 

     


            
get  

            


                 
return  itemPrice; 

            }
 

            
set  

            


                 itemPrice 
=  value; 

            }
 

     }
 

     
public   long  ItemQuantity 

     


            
get  

            


                 
return  itemQuantity; 

            }
 

            
set  

            


                 itemQuantity 
=  value; 

            }
 

     }
 

     
public   double  NetAmount 

     


            
get  

            


                 
return  netAmount; 

            }
 

            
set  

            


                 netAmount 
=  value; 

            }
 

     }
 

}
 


// Create a file stream in which teh object will store 

FileStream fstream 
=  File.Create(SERIALIZED_FILE_PATH  +  @ & quot;\ & quot;  +  objItem.ItemId); 

// Object will store in binary format 

BinaryFormatter bf 
=   new  BinaryFormatter(); 

// Bind the filestream with that of binaryformatter. Also mentioned the object to save 

bf.Serialize(fstream, objItem); 

fstream.Close(); 

fstream 
=   null

bf 
=   null

// ---------------------------- 
FileStream fstream  =  File.OpenRead (serializedFiles.GetValue(fileIndex).ToString() ); 

BinaryFormatter bf 
=   new  BinaryFormatter(); 

Item ser 
=  (Item) bf.Deserialize(fstream); 

posted on 2006-03-23 17:21 梦在天涯 阅读(977) 评论(3)  编辑 收藏 引用 所属分类: C#/.NET

评论

# re: object 序列化 2006-04-14 12:09 梦在天涯

class must have

public members or getandset of properities,

then it can be serialized.  回复  更多评论   

# re: object 序列化 2006-04-14 16:17 梦在天涯

NET的对象序列化真是好东西,以后要多多利用。

  在使用.NET的序列化时,碰到过一些问题,还好,有丰富的MSDN可查,没有什么过不去的槛。在这里,把使用.NET序列化的经验小结一下。
  1. 基本确认XmlSerializer使用UTF8对序列化的XML文档编码。
  2. XmlSerializer只序列化声明为public的字段,属性,或带返回值的方法。
  3. 如果要序列化属性,那么该属性必须是可读写的,即必须包含get和set,而不能是readonly或writeonly。
  4. XmlAttribute,XmlAnyAttribute不能与XmlElement,XmlText,XmlAnyElement,XmlArray,XmlArrayItem一起使用。
  5. XmlRoot只能用于一个类,XmlType可用于所有类。
  6. 不同的类的XmlType不能相同,除非使用NameSpaces区分。

相关链接:
  ● 在.NET中实现对象序列化
  ● 对象序列化:使用System.Xml.Serialization命名空间
  ● 对象序列化:使用XmlSerializer走完最后一步  回复  更多评论   

# re: object 序列化 2008-03-08 12:39 爱情白面包

不错!!!
顶一下!  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783914
  • 排名 - 5

最新评论

阅读排行榜