序列化之XML序列化(一)

MSDN上的定义:
XML 序列化将对象的公共字段和属性或者方法的参数和返回值转换(序列化)为符合特定 XML 架构定义语言 (XSD) 文档的 XML 流。XML 序列化生成强类型的类,并为存储或传输目的将其公共属性和字段转换为序列格式(在此情况下为 XML)简单的说就是将一个对象转换成XML流或是文件的过程。
注意事项:要序列化的类必须有一个默认的构造器,不能序列化类中的私有变量或方法。
下面以一个实例来说明。
这个是要序列化的类。

None.gif      public   class  XmlSerObject
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public XmlSerObject()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private DateTime m_datetime;
InBlock.gif        
public DateTime CurrentDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_datetime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.m_datetime = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string m_name;
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn this.m_name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gifthis.m_name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public XmlSerObject(string name, DateTime current)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.m_name = name;
InBlock.gif            
this.m_datetime = current;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif

这个是我写的一个方法用来序列化这个类并将数据保存到一个xml文件中。

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 序列化一个对象
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fileName">要存放的文件名(包括路径)</param>
ExpandedBlockEnd.gif        
/// <param name="xmlobj">被序列化的对象</param>

None.gif          public   static   void  SerObject( string  fileName, Object xmlobj)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
using (FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//得到被序列化的类型
InBlock.gif
                Type type = xmlobj.GetType();
InBlock.gif                XmlSerializer sz 
= new XmlSerializer(type);
InBlock.gif                
//开始序列化
InBlock.gif
                sz.Serialize(stream, xmlobj);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

通过这个方法就可以生成一个XML文件

None.gif          private   void  btnXml_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
//创建一个对象。
InBlock.gif
            XmlSerObject serobj = new XmlSerObject("fastyou", DateTime.Now);
InBlock.gif            
//XmlSerialize这个是SerObject这个方法所在的类名。
InBlock.gif
            XmlSerialize.SerObject(@"c:\fastyou.xml", serobj);
ExpandedBlockEnd.gif        }

接着我们来打开这个xml文件来看看结果。

None.gif <? xml version="1.0" ?>
None.gif
< XmlSerObject  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
None.gif  
< CurrentDate > 2006-11-14T00:53:09.5731779+08:00 </ CurrentDate >
None.gif  
< Name > fastyou </ Name >
None.gif
</ XmlSerObject >

这个就是我们序列化后看到的结果。如果你在要序列化的类名加上[XmlRoot("Fastyou")]这个Attribute的话那么XML文件里的XmlSerObject将会变成Fastyou.^_^.如果在类的Name属性[XmlAttribute("Name")]这个Attribute的话结果又会有变化请看下面Name是不是换了位置呢。

None.gif <? xml version="1.0" ?>
None.gif
< XmlSerObject  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  Name ="fastyou" >
None.gif  
< CurrentDate > 2006-11-14T00:57:09.0278354+08:00 </ CurrentDate >
None.gif
</ XmlSerObject >

如果你将序列化的方法改成下面的形式就会得到这个xml文件的内容的一个字符串

ExpandedBlockStart.gif ContractedBlock.gif          /**/ /// <summary>
InBlock.gif        
/// 序列化一个对象
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="fileName">要存放的文件名(包括路径)</param>
ExpandedBlockEnd.gif        
/// <param name="xmlobj">被序列化的对象</param>
None.gif          public   static   string  SerObject( string  fileName, Object xmlobj)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder sb 
= new StringBuilder();
InBlock.gif            StringWriter tw 
= new StringWriter(sb);
InBlock.gif            Type type 
= xmlobj.GetType();
InBlock.gif            XmlSerializer sz 
= new XmlSerializer(type);
InBlock.gif            sz.Serialize(tw, xmlobj);
InBlock.gif            tw.Close();
InBlock.gif            
return sb.ToString();
ExpandedBlockEnd.gif        }

这是一个简单的XML序列的实例。有时间再写更深入一点的有关XML序列化的文章。
调试环境:xp sp2,vs2005

转载于:https://www.cnblogs.com/laihua/archive/2006/11/14/559851.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值