旗下导航:搜·么
当前位置:网站首页 > XML教程 > 正文

应用XMLSerializer将对象串行化到XML【XML教程】,XMLSerializer,对象串行化,XML

作者:搜教程发布时间:2019-12-01分类:XML教程浏览:31评论:0


导读:微软已意想到串行化数据的重要性,因而在.NET框架中包含了定名空间System.Runtime.Serialization和System.xml.Serialization以供应串...
微软已意想到串行化数据的重要性,因而在.NET框架中包含了定名空间System.Runtime.Serialization和System.xml.Serialization以供应串行化功用,为用户本身编写串行化要领供应了一个框架。System.Xml.Serialization定名空间供应了将一个对象串行化为XML花样的基础要领。下面我们来看看怎样运用这类要领。

XML的魅力

串行化XML是指为了轻易存储或传输,把一个对象的大众的域和属性保留为一种串行花样(这里是XML花样)的历程。非串行化则是运用串行的状况信息将对象从串行XML状况还原成原始状况的历程。因而,能够把串行化看做是将对象的状况保留到流或缓冲区中的一种要领。

串行化的目标是数据存储和数据转换。数据存储指的是在用户会话时保留数据。当应用顺序封闭时,数据被保留(串行化),而当用户回来时,数据又被从新加载(非串行化)。数据转换指将数据变换成能被另一个体系辨认的花样。运用串行化和XML,能够很轻易的举行数据转换。

对象中的数据能够是类、要领、属性、私有范例、数组,在System.Xml.XmlElement或System.Xml.XmlAttribute对象中,它以至能够是内嵌的XML。

System.Xml.Serialization定名空间中的症结类是XmlSerializer。当然在该定名空间中还包含有关XML其他方面以及SOAP相干的其他类,不过我们的重点是XmlSerializer类。

XmlSerializer
XmlSerializer类供应了把对象串行化为XML文件及把XML文档非串行化为对象的要领。它还能让用户指定对象怎样转化为XML。能够把将被串行化的对象的范例作为类组织函数的参数。下面的C# 代码说清楚明了组织函数的用法。

    XmlSerializer ser = new XmlSerializer(typeof(objectToSerialize));

下面是等价的VB.NET代码:

    Dim ser As New XmlSerializer(GetType(objectToSerialize))

现实的串行化历程在XmlSerializer类的Serialize要领中完成。该要领许可在串行化历程当中挪用TextWriter、Stream和XmlWriter对象。下面的例子代码说清楚明了怎样挪用该要领。在本例中一个对象被串行化保留到当地磁盘的一个文件当中。例子中首先是类声明,背面紧接着是串行化源代码。

using System;
namespace BuilderSerialization {
public class Address {
public Address() {}
public string Address1;
public string Address2;
public string City;
public string State;
public string Zip;
public string Country;
} }
using System;
namespace BuilderSerialization {
public class Author {
public Author() { }
public string FirstName;
public string MiddleName;
public string LastName;
public string Title;
public string Gender;
public Address AddressObject;
} }
namespace BuilderSerialization {
public class Book {
public Book() { }
public string Title;
public Author AuthorObject;
public string ISBN;
public double RetailPRice;
public string Publisher;
}}
using System;
using System.Xml.Serialization;
using System.IO;
namespace BuilderSerialization {
class TestClass {
static void Main(string[] args) {
Book BookObject = new Book();
XmlSerializer ser = new XmlSerializer(typeof(Book));
TextWriter writer = new StreamWriter("booktest.xml");
BookObject.Title = "Practical LotusScript";
BookObject.ISBN = "1884777767 ";
BookObject.Publisher = "Manning Publications";
BookObject.RetailPrice = 43.95;
BookObject.AuthorObject = new Author();
BookObject.AuthorObject.FirstName = "Tony";
BookObject.AuthorObject.LastName = "Patton";
BookObject.AuthorObject.Gender = "Male";
BookObject.AuthorObject.AddressObject = new Address();
BookObject.AuthorObject.AddressObject.Address1 = "1 Main Street";
BookObject.AuthorObject.AddressObject.City = "Anywhere";
BookObject.AuthorObject.AddressObject.State = "KY";
BookObject.AuthorObject.AddressObject.Zip = "40000";
BookObject.AuthorObject.AddressObject.Country = "USA";
ser.Serialize(writer, BookObject);
writer.Close();
} } }

上面的代码把三个对象变成一个对象,因而在串行化历程当中发生一个XML文件。以下是例子顺序发生的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Title>Practical LotusScript</Title>
<AuthorObject>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<ISBN>1884777767 </ISBN>
<RetailPrice>43.95</RetailPrice>
<Publisher>Manning Publications</Publisher>
</Book>

注重串行化历程也能处置惩罚对象数据的嵌套。数据被转换成可辨认的花样,轻易了数据重载(非串行化)以及向另一个体系的数据传输。在数据传输历程时,接收方体系须要晓得XML文件的花样(假如预先不晓得的话)。因而须要供应一个XML schema文件。.NET框架中的XSD.exe东西能够为串行化XML生成一个schema文件。

下面是用VB.NET编写的例子代码:

Public Class Address
Public Address1 As String
Public Address2 As String
Public City As String
Public State As String
Public Zip As String
Public Country As String
End Class
Public Class Author
Public FirstName As String
Public MiddleName As String
Public LastName As String
Public Title As String
Public Gender As String
Public AddressObject As Address
End Class
Public Class Book
Public AuthorObject As Author
Public Title As String
Public ISBN As String
Public RetailPrice As Double
Public Publisher As String
End Class
Imports System.Xml.Serialization
Imports System.IO
Module Module1
Sub Main()
Dim BookObject As New Book
Dim ser As New XmlSerializer(GetType(Book))
Dim writer As New StreamWriter("booktest.xml")
With BookObject
.Title = "Practical LotusScript"
.ISBN = "1884777767 "
.Publisher = "Manning Publications"
.RetailPrice = 43.95
.AuthorObject = New Author
.AuthorObject.FirstName = "Tony"
.AuthorObject.LastName = "Patton"
.AuthorObject.Gender = "Male"
.AuthorObject.AddressObject = New Address
.AuthorObject.AddressObject.Address1 = "1 Main Street"
.AuthorObject.AddressObject.City = "Anywhere"
.AuthorObject.AddressObject.State = "KY"
.AuthorObject.AddressObject.Zip = "40000"
.AuthorObject.AddressObject.Country = "USA"
End With
ser.Serialize(writer, BookObject)
writer.Close()
End Sub
End Module

掌握输出

串行化历程生成规范的XML文件,数据成员转换为XML元素。不过,并不是一切的数据成员都变成元素,能够经由过程在类代码中增加一些标记来掌握输出的XML文件。如许,数据成员能够变换为XML属性而非元素,也能够简朴的被疏忽掉。下面的例子是一段经由修改后的book类VB.NET代码。

Public Class Book
Public AuthorObject As Author
Public Title As String
<System.Xml.Serialization.XmlAttribute()> _
Public ISBN As String
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public RetailPrice As Double
Public Publisher As String
End Class

这段代码通知体系在生成XML文件时把类成员ISBN作为XML属性,同时疏忽掉RetailPrice成员。这类变化能够在生成的XML文件中能够看出:

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ISBN="1884777767 ">
<AuthorObject>
<FirstName>Tony</FirstName>
<LastName>Patton</LastName>
<Gender>Male</Gender>
<AddressObject>
<Address1>1 Main Street</Address1>
<City>Anywhere</City>
<State>KY</State>
<Zip>40000</Zip>
<Country>USA</Country>
</AddressObject>
</AuthorObject>
<Title>Practical LotusScript</Title>
<Publisher>Manning Publications</Publisher>
</Book>

以下是响应的C# 代码:

public class Book {
public Book() { }
public string Title;
public Author AuthorObject;
[System.Xml.Serialization.XmlAttribute()]
public string ISBN;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public double RetailPrice;
public string Publisher;
}

上面仅是轻微提了两种标记符号。请查阅.NET文档以取得完全的标记符号。

非串行化
非串行化数据经由过程挪用XmlSerializer类的Deserialize要领能够轻易地完成。以下的VB.NET顺序片段完成上文中XML文档的非串行化:

Dim BookObject As New Book
Dim ser As New XmlSerializer(GetType(Book))
Dim fs As New System.IO.FileStream("booktest.xml", FileMode.Open)
Dim reader As New System.XML.XmlTextReader(fs)
BookObject = CType(ser.Deserialize(reader), Book)
该顺序把效果数据放入内存备用。下面是等价的C# 代码:
XmlSerializer ser = new XmlSerializer(typeof(Book));
System.IO.FileStreamfs = new System.IO.FileStream("booktest.xml",
FileMode.Open);
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(fs);
Book BookObject = (Book)(ser.Deserialize(reader));

以上就是应用XMLSerializer将对象串行化到XML的内容,更多相干内容请关注ki4网(www.ki4.cn)!

标签:XMLSerializer对象串行化XML


欢迎 发表评论: