zoukankan
html css js c++ java
.Net下的XML序列化(一)
XML序列化可以让你使用class-friendly的方式操作XML。我们可以方便的将某个类序列化成XML字符串或文件,这里是一个例子。
Address类:
[Serializable]
public
class
Address
{
public
Address()
{}
public
string
Street
{
get
{
return
street; }
set
{ street
=
value; }
}
private
string
street;
public
string
City
{
get
{
return
city; }
set
{ city
=
value; }
}
private
string
city;
public
string
State
{
get
{
return
state; }
set
{ state
=
value; }
}
private
string
state;
}
Customer类:
[Serializable]
public
class
Customer
{
public
Customer()
{}
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
private
string
name;
public
Address Address
{
get
{
return
address; }
set
{ address
=
value; }
}
private
Address address;
}
必须在将要序列化的类上加入特性[Serializable]
生成测试数据:
public
static
Customer GetGustomer()
{
Customer customer
=
new
Customer();
Address address
=
new
Address();
address.City
=
"
北京
"
;
address.State
=
"
丰台
"
;
address.Street
=
"
马家堡西里
"
;
customer.Address
=
address;
customer.Name
=
"
BillChen
"
;
return
customer;
}
进行序列化操作。
[STAThread]
static
void
Main(
string
[] args)
{
Customer customer
=
Customers.GetGustomer();
SerializerCustomer1(customer);
SerializerCustomer2(customer);
SerializerCustomer3(customer);
Console.ReadLine();
}
private
static
void
SerializerCustomer1(Customer customer)
{
XmlSerializer ser
=
new
XmlSerializer(
typeof
(Customer));
FileStream stream
=
new
FileStream(
"
test.xml
"
, FileMode.OpenOrCreate);
ser.Serialize( stream, customer );
stream.Close();
}
private
static
void
SerializerCustomer2(Customer customer)
{
XmlSerializer ser
=
new
XmlSerializer(
typeof
(Customer));
MemoryStream stream
=
new
MemoryStream(
100
);
ser.Serialize( stream, customer );
stream.Position
=
0
;
using
(StreamReader reader
=
new
StreamReader(stream, Encoding.UTF8))
{
Console.Write(reader.ReadToEnd());
}
}
private
static
void
SerializerCustomer3(Customer customer)
{
XmlSerializer ser
=
new
XmlSerializer(
typeof
(Customer));
MemoryStream stream
=
new
MemoryStream(
100
);
XmlTextWriter writer
=
new
XmlTextWriter(stream, Encoding.UTF8);
writer.Formatting
=
Formatting.Indented;
//
缩进
ser.Serialize( writer, customer );
stream.Position
=
0
;
using
(StreamReader reader
=
new
StreamReader(stream, Encoding.UTF8))
{
string
line;
while
((line
=
reader.ReadLine())
!=
null
)
{
Console.WriteLine(line);
}
}
writer.Close();
}
以上是序列化指定的类,及读取序列化后的XML内容的几种方式。
查看全文
相关阅读:
Teched最后两天下载,同时新加熊老的teched录像,请尽快下载。
如何学习,牛人是否真牛?
为什么我的脚本大多是支持IE环境
SPS中提供的Blog
teched2004最后一天下载,新增js的menu1.0下载
asp+xml+js所作的文件管理器,完全仿xp风格,精彩下载不要错过。
将业务系统数据库的数据显示在页面上并且作WebPart的跨页面连接
Activity中UI框架基本概念
Android学习笔记——Activity的启动和创建
Mms模块ConversationList流程分析(2)
原文地址:https://www.cnblogs.com/chenjunbiao/p/1760275.html
最新文章
孩子相关的网站
[Salesforce] [ADM201] Customizing Chatter Feed Tracking
C#仿QQ皮肤-主窗体MainForm和Main的实现[转]
完整的SQL注入语句[转]
Microsoft .Net Remoting系列专题之二:Marshal、Disconnect与生命周期以及跟踪服务[转]
C# 操作Excel,控制格式[转]
Microsoft .Net Remoting系列专题之三:Remoting事件处理全接触[转]
.net实现客户区延伸至至非客户区 [转]
远程桌面命令[转]
C# Winform 窗体操作
热门文章
压缩日志及数据库文件大小[转]
C# 模仿360安全卫士玻璃按钮(源码)[转]
WinForm怎么在窗口标题栏上添加按钮?[转]
Windows Message ID[转]
Microsoft .Net Remoting系列专题之一:.Net Remoting基础篇[转]
超高性价比手机选购[转]
关于全角转半角的方法[转]
大力宣传经典之作,XMLMenu1.61,玩JS的不下载的绝对后悔。另外还有xmltree2.1
什么都不能大意,特别是我们做IT的。
teched2004视频资料下载,又加了5段,全是开发类的
Copyright © 2011-2022 走看看