要使用.NET进行对象的序列化,必须在解决方案中添加System.Xml的引用,并且在类文件中引入System.Xml.Serialization命名空间。这样就可以在文件中使用序列化所需要的各种特性了。
如果对XML Serialization缺少了解,请首先参考拙文: 在.NET中实现对象序列化
上面的例子包含了典型的XML中常见的各种元素:XML声明、XML根节点、XML节点、XML属性、XML集合。除XML声明外,在.NET中都有对应的特性用于定义这些元素。这些特性包括:XmlRootAttribute、XmlTypeAttribute、XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute和XmlArrayItemAttribute。另外,还有两个常用的特性,XmlIgnoreAttribute用于标记在对象序列化时需要被忽略的部分,XmlIncludeAttribute用于标记在生成XML Schema时需要包括的类型。
如果没有显式地标记任何特性,那么默认类的特性为XmlTypeAttribute、类成员的特性为XmlElementAttribute,且名称为类或类成员的名称。例如:
如果不做任何特性标记,使用下面的代码序列化时:
序列化后的XML为:
可以看到,<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方法。
要将类序列化为XML节点:
要将类序列化为XML根节点:
当在类中同时使用XmlRootAttribute、XmlTypeAttribute时,序列化文档中的类型以XmlRootAttribute为准:
要将类成员序列化为XML节点:
要将类成员序列化为XML属性:
要将类成员序列化为XML集合:
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 >
< 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
Class Order
Public ID As String
Public OrderDate As String
End Class
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)
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 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)
ns.Add( "" , "" )
writer.Formatting = Formatting.Indented
serializer.Serialize(writer, o, ns)
要将类序列化为XML节点:
<
XmlType(
"
order
"
)
>
_
Public Class Order Class Order
‘ any code here.
End Class
Public Class Order Class Order
‘ any code here.
End Class
要将类序列化为XML根节点:
<
XmlRoot(
"
order
"
)
>
_
Public Class Order Class Order
‘ any code here.
End Class
Public Class Order Class Order
‘ any code here.
End Class
当在类中同时使用XmlRootAttribute、XmlTypeAttribute时,序列化文档中的类型以XmlRootAttribute为准:
<
XmlRoot(
"
order
"
), XmlType(
"
anotherOrderName
"
)
>
_
Public Class Order Class Order
‘ any code here.
End Class
Public Class Order Class Order
‘ any code here.
End Class
要将类成员序列化为XML节点:
<
XmlAttributeAttribute(
"
id
"
)
>
_
Public ID As String
Public ID As String
要将类成员序列化为XML属性:
<
XmlAttributeAttribute(
"
id
"
)
>
_
Public ID As String
Public ID As String
要将类成员序列化为XML集合:
<
XmlRoot(
"
order
"
), XmlType(
"
anotherOrderName
"
)
>
_
Public Class Order 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 Class OrderItem
Public Name As String
End Class
Public Class Order 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 Class OrderItem
Public Name As String
End Class