.Net下的XML序列化(一)

XML序列化可以让你使用class-friendly的方式操作XML。我们可以方便的将某个类序列化成XML字符串或文件,这里是一个例子。

Address类:
None.gif     [Serializable]
None.gif    
public   class  Address
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Address()dot.gif{}
InBlock.gif
InBlock.gif
InBlock.gif        
public string Street
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn street; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ street = value; }
ExpandedSubBlockEnd.gif        }
private string street;
InBlock.gif
InBlock.gif        
public string City
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn city; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ city = value; }
ExpandedSubBlockEnd.gif        }
private string city;
InBlock.gif
InBlock.gif        
public string State
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn state; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ state = value; }
ExpandedSubBlockEnd.gif        }
private string state;
ExpandedBlockEnd.gif    }

Customer类:
None.gif     [Serializable]
None.gif    
public   class  Customer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public Customer()dot.gif{}
InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ name = value; }
ExpandedSubBlockEnd.gif        }
private string name;
InBlock.gif
InBlock.gif        
public Address Address
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ address = value; }
ExpandedSubBlockEnd.gif        }
private Address address;
ExpandedBlockEnd.gif    }

必须在将要序列化的类上加入特性[Serializable]

生成测试数据:
None.gif          public   static  Customer GetGustomer()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Customer customer 
= new Customer();
InBlock.gif            Address address 
= new Address();
InBlock.gif            address.City 
= "北京";
InBlock.gif            address.State 
= "丰台";
InBlock.gif            address.Street 
= "马家堡西里";
InBlock.gif            
InBlock.gif            customer.Address 
= address;
InBlock.gif            customer.Name 
= "BillChen";
InBlock.gif
InBlock.gif            
return customer;
ExpandedBlockEnd.gif        }

进行序列化操作。
None.gif         [STAThread]
None.gif        
static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Customer customer 
= Customers.GetGustomer();
InBlock.gif
InBlock.gif            SerializerCustomer1(customer);
InBlock.gif            SerializerCustomer2(customer);
InBlock.gif            SerializerCustomer3(customer);
InBlock.gif
InBlock.gif            Console.ReadLine();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   static   void  SerializerCustomer1(Customer customer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            XmlSerializer ser 
= new XmlSerializer(typeof(Customer));
InBlock.gif            FileStream stream 
= new FileStream("test.xml", FileMode.OpenOrCreate);
InBlock.gif
InBlock.gif            ser.Serialize( stream, customer );
InBlock.gif
InBlock.gif            stream.Close();
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   static   void  SerializerCustomer2(Customer customer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            XmlSerializer ser 
= new XmlSerializer(typeof(Customer));
InBlock.gif            
InBlock.gif            MemoryStream stream 
= new MemoryStream(100);
InBlock.gif            ser.Serialize( stream, customer );
InBlock.gif
InBlock.gif            stream.Position 
= 0;
InBlock.gif
            using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                Console.Write(reader.ReadToEnd());
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif        
private   static   void  SerializerCustomer3(Customer customer)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            XmlSerializer ser 
= new XmlSerializer(typeof(Customer));
InBlock.gif            
InBlock.gif            MemoryStream stream 
= new MemoryStream(100);
InBlock.gif            XmlTextWriter writer 
= new XmlTextWriter(stream, Encoding.UTF8);
InBlock.gif            writer.Formatting 
= Formatting.Indented;//缩进
InBlock.gif
            ser.Serialize( writer, customer );
InBlock.gif
InBlock.gif            stream.Position 
= 0;
InBlock.gif
            using(StreamReader reader = new StreamReader(stream, Encoding.UTF8))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string line;
InBlock.gif                
while((line = reader.ReadLine()) != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(line);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            writer.Close();
ExpandedBlockEnd.gif        }

以上是序列化指定的类,及读取序列化后的XML内容的几种方式。

转载于:https://www.cnblogs.com/BillChen/archive/2005/08/27/223992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值