对象序列化:使用System.Xml.Serialization命名空间(转)

        要使用.NET进行对象的序列化,必须在解决方案中添加System.Xml的引用,并且在类文件中引入 System.Xml.Serialization命名空间。这样就可以在文件中使用序列化所需要的各种特性了。
Imports  System.Xml.Serialization

  如果对XML Serialization缺少了解,请首先参考拙文: 在.NET中实现对象序列化
<? xml version="1.0" encoding="utf-8" ?>
< order  id =”123456”> 
    
<orderDate > 2005-04-05 </ orderDate >  
    
< items >  
        
< item >  
            
< name > 对象序列化 </ name >  
        
</ item >  
    
</ items >  
</ order >

   上面的例子包含了典型的XML中常见的各种元素:XML声明、XML根节点、XML节点、XML属性、XML集合。除XML声明外,在.NET中都有对应的特性用于定义这些元素。这些特性包括:XmlRootAttribute、XmlTypeAttribute、XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute和XmlArrayItemAttribute。另外,还有两个常用的特性,XmlIgnoreAttribute用于标记在对象序列化时需要被忽略的部分,XmlIncludeAttribute用于标记在生成XML Schema时需要包括的类型。

   如果没有显式地标记任何特性,那么默认类的特性为XmlTypeAttribute、类成员的特性为XmlElementAttribute,且名称为类或类成员的名称。例如:
Public   Class Order
        
Public ID As String
        
Public OrderDate As String
   
End Class

  如果不做任何特性标记,使用下面的代码序列化时:
         Dim  o  As   New  Order
        
With  o
            .ID 
=   123456
            .OrderDate 
=   Date .Now.ToShortDateString
        
End   With

        
Dim  writer  As   New  XmlTextWriter( " abc.xml " , Encoding.UTF8)
        
Dim  serializer  As   New  XmlSerializer( GetType (Order))

        writer.Formatting 
=  Formatting.Indented
        serializer.Serialize(writer, o)

  序列化后的XML为:
<? xml version="1.0" encoding="utf-8" ?>
< Order  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" >
  
< ID > 123456 </ ID >
  
< OrderDate > 2005-4-11 </ OrderDate >
</ Order >

  可以看到,<Order>对应Order类,而<ID>和<OrderDate>分别对应Order类中的字段ID和OrderDate。另外,多了一个XML声明和两个XML命名空间。
  XML声明是.NET自动添加的,但是encoding是在XmlTextWriter中指定的,如果不指定encoding,那么XML声明只有<?xml version="1.0"?>。我使用的是.NET 1.1,这个版本中只支持XML 1.0版本。另外,如果不指定encoding,那么默认的编码可能也是UTF8(没找到相关的资料)。
  .NET默认为Order类添加了XMLSchema和XMLSchema-instance两个W3C的命名空间。该命名空间也可以自己指定,方法是使用XmlSerializer的另一个Serialize方法。
             Dim  ns  As   New  XmlSerializerNamespaces
            ns.Add(
"" "" )

            writer.Formatting 
=  Formatting.Indented
            serializer.Serialize(writer, o, ns)

  要将类序列化为XML节点:
     < XmlType( " order " ) >  _
    
Public   Class Order
        ‘ any code here.
    
End Class

  要将类序列化为XML根节点:
     < XmlRoot( " order " ) >  _
    
Public   Class Order
        ‘ any code here.
    
End Class

  当在类中同时使用XmlRootAttribute、XmlTypeAttribute时,序列化文档中的类型以XmlRootAttribute为准:
     < XmlRoot( " order " ), XmlType( " anotherOrderName " ) >  _
    
Public   Class Order
        ‘ any code here.
    
End Class

  要将类成员序列化为XML节点:
         < XmlAttributeAttribute( " id " ) >  _
        
Public  ID  As   String

  要将类成员序列化为XML属性:
         < XmlAttributeAttribute( " id " ) >  _
        
Public  ID  As   String

  要将类成员序列化为XML集合:
     < XmlRoot( " order " ), XmlType( " anotherOrderName " ) >  _
    
Public   Class Order
        
<XmlAttributeAttribute("id")> _
        
Public ID As String
        
Public orderDate As String
        
<XmlArray("items"), XmlArrayItem("item", Type:=GetType(OrderItem))> _
        
Public Items As New ArrayList
    
End Class


    
< XmlType( " orderItem " ) >  _
    
Public   Class OrderItem
        
Public Name As String
    
End Class

  使用特性的一个好处是: 可以在代码和序列化的文档中使用不同的编码规范。 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值