一、什么是序列化和反序列化
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
二、XmlSerializer对象的xml序列化和反序列化
1)简单的xml序列化
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Program
{
static
void
Main(
string
[] args)
{
int
i = 10;
//声明Xml序列化对象实例serializer
//指定类型
XmlSerializer serializer =
new
XmlSerializer(
typeof
(
int
));
//执行序列化并将序列化结果输出到控制台
serializer.Serialize(Console.Out, i);
Console.Read();
}
}
|
<?
xml
version="1.0" encoding="gb2312"?>
<
int
>10</
int
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
static
void
Main(
string
[] args)
{
using
(StringReader rdr =
new
StringReader(
@"<?xml version=""1.0"" encoding=""gb2312""?>
<int>10</int>"
))
{
//声明序列化对象实例serializer
XmlSerializer serializer =
new
XmlSerializer(
typeof
(
int
));
//反序列化,并将反序列化结果值赋给变量i
int
i = (
int
)serializer.Deserialize(rdr);
//输出反序列化结果
Console.WriteLine(
"i = "
+ i);
Console.Read();
}
}
|
namespace
UseXmlSerialization
{
class
Program
{
static
void
Main(
string
[] args)
{
//声明一个猫咪对象
var
c =
new
Cat { Color =
"White"
, Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
//序列化这个对象
XmlSerializer serializer =
new
XmlSerializer(
typeof
(Cat));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, c);
Console.Read();
}
}
[XmlRoot(
"cat"
)]
public
class
Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute(
"color"
)]
public
string
Color {
get
;
set
; }
//要求不序列化Speed属性
[XmlIgnore]
public
int
Speed {
get
;
set
; }
//设置Saying属性序列化为Xml子元素
[XmlElement(
"saying"
)]
public
string
Saying {
get
;
set
; }
}
}
namespace
UseXmlSerialization
{
class
Program
{
static
void
Main(
string
[] args)
{
//声明一个猫咪对象
var
c =
new
Cat { Color =
"White"
, Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
//序列化这个对象
XmlSerializer serializer =
new
XmlSerializer(
typeof
(Cat));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, c);
Console.Read();
}
}
[XmlRoot(
"cat"
)]
public
class
Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute(
"color"
)]
public
string
Color {
get
;
set
; }
//要求不序列化Speed属性
[XmlIgnore]
public
int
Speed {
get
;
set
; }
//设置Saying属性序列化为Xml子元素
[XmlElement(
"saying"
)]
public
string
Saying {
get
;
set
; }
}
}
namespace
UseXmlSerialization
{
class
Program
{
static
void
Main(
string
[] args)
{
//声明一个猫咪对象
var
c =
new
Cat { Color =
"White"
, Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
//序列化这个对象
XmlSerializer serializer =
new
XmlSerializer(
typeof
(Cat));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, c);
Console.Read();
}
}
[XmlRoot(
"cat"
)]
public
class
Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute(
"color"
)]
public
string
Color {
get
;
set
; }
//要求不序列化Speed属性
[XmlIgnore]
public
int
Speed {
get
;
set
; }
//设置Saying属性序列化为Xml子元素
[XmlElement(
"saying"
)]
public
string
Saying {
get
;
set
; }
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Xml.Serialization;
namespace
UseXmlSerialization
{
class
Program
{
static
void
Main(
string
[] args)
{
//声明一个猫咪对象
var
cWhite =
new
Cat { Color =
"White"
, Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
var
cBlack =
new
Cat { Color =
"Black"
, Speed = 10, Saying =
"White or black, so long as the cat can catch mice, it is a good cat"
};
CatCollection cc =
new
CatCollection { Cats =
new
Cat[] { cWhite,cBlack} };
//序列化这个对象
XmlSerializer serializer =
new
XmlSerializer(
typeof
(CatCollection));
//将对象序列化输出到控制台
serializer.Serialize(Console.Out, cc);
Console.Read();
}
}
[XmlRoot(
"cats"
)]
public
class
CatCollection
{
[XmlArray(
"items"
),XmlArrayItem(
"item"
)]
public
Cat[] Cats {
get
;
set
; }
}
[XmlRoot(
"cat"
)]
public
class
Cat
{
//定义Color属性的序列化为cat节点的属性
[XmlAttribute(
"color"
)]
public
string
Color {
get
;
set
; }
//要求不序列化Speed属性
[XmlIgnore]
public
int
Speed {
get
;
set
; }
//设置Saying属性序列化为Xml子元素
[XmlElement(
"saying"
)]
public
string
Saying {
get
;
set
; }
}
}
控制台输出结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?
xml
version="1.0" encoding="gb2312"?>
<
cats
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://ww
w.w3.org/2001/XMLSchema">
<
items
>
<
item
color="White">
<
saying
>White or black, so long as the cat can catch mice, it is a good
cat</
saying
>
</
item
>
<
item
color="Black">
<
saying
>White or black, so long as the cat can catch mice, it is a good
cat</
saying
>
</
item
>
</
items
>
</
cats
>
|