首页
最近更新
热门教程
ASP.NET
开发语言
开发环境
AJax教程
控件开发
统计报表
数据库
Web服务
安装部署
HTML教程
Javascript
XML教程
Community Server
NHibernate
书籍推荐
常用工具
实用代码
教程全文搜索
首页
>>
ASP.NET
使用Xml序列化和反序列化
添加人:
iyond
添加时间:2007-12-04 阅读次数:1004
收藏此教程
以前写的Xml序列化和反序列化的代码,希望对大家有些帮助
1
using
System;
2
using
System.IO;
3
using
System.Xml;
4
using
System.Xml.Serialization;
5
using
System.Collections;
6
using
System.Web;
7
using
System.Web.Caching;
8
using
System.Text;
9
using
System.Configuration;
10
11
namespace
Cutech.AddressBook.Configuration
12
{
13
public
class
ModuleConfig
14
{
15
public
static
ModuleSettings GetSettings()
16
{
17
HttpContext context
=
HttpContext.Current;
18
ModuleSettings data
=
(ModuleSettings)context.Cache[
"
AddressBook_Settings
"
];
19
if
(data
==
null
)
20
{
21
XmlSerializer serializer
=
new
XmlSerializer(
typeof
(ModuleSettings));
22
try
23
{
24
string
fileName
=
HttpContext.Current.Server.MapPath(GetSettingsFile());
25
//
create a filestream to read the XML document
26
FileStream fs
=
new
FileStream(fileName, FileMode.Open);
27
//
use the Deserialize method to retrieve the oject state
28
data
=
(ModuleSettings)serializer.Deserialize(fs);
29
fs.Close();
30
31
context.Cache.Insert(
"
AddressBook_Settings
"
, data,
new
CacheDependency(fileName));
32
}
33
catch
(System.IO.FileNotFoundException)
34
{
35
//
if the file is not found, return a new empty class
36
data
=
new
ModuleSettings();
37
}
38
}
39
40
return
data;
41
}
42
43
public
static
void
SaveSettings(ModuleSettings data)
44
{
45
string
fileName
=
HttpContext.Current.Server.MapPath(GetSettingsFile());
46
XmlSerializer serializer
=
new
XmlSerializer(
typeof
(ModuleSettings));
47
48
//
serialize the object
49
FileStream fs
=
new
FileStream(fileName, FileMode.Create);
50
serializer.Serialize(fs, data);
51
fs.Close();
52
}
53
54
private
static
string
GetSettingsFile()
55
{
56
HttpContext context
=
HttpContext.Current;
57
//
get the file path from the cache
58
string
filePath
=
(
string
)context.Cache[
"
AddressBook_SettingsFile
"
];
59
//
if path is null, get it from web.config
60
if
(filePath
==
null
)
61
{
62
//
retrieve the value
63
filePath
=
ConfigurationSettings.AppSettings[
"
AddressBook_SettingsFile
"
];
64
//
save into the cache
65
context.Cache[
"
AddressBook_SettingsFile
"
]
=
filePath;
66
}
67
//
return the path
68
return
filePath;
69
}
70
}
71
72
[Serializable()]
73
public
class
ModuleSettings
74
{
75
private
string
connectionString;
76
private
string
strMoneyUrl;
77
private
string
strLoginUrl;
78
private
string
strServiceID;
79
80
public
string
ConnectionString
81
{
82
get
{
return
connectionString; }
83
set
{ connectionString
=
value; }
84
}
85
public
string
MoneyUrl
86
{
87
get
{
return
strMoneyUrl; }
88
set
{ strMoneyUrl
=
value; }
89
}
90
public
string
LoginUrl
91
{
92
get
{
return
strLoginUrl; }
93
set
{ strLoginUrl
=
value; }
94
}
95
public
string
ServiceID
96
{
97
get
{
return
strServiceID; }
98
set
{ strServiceID
=
value; }
99
}
100
}
101
}
共
1
页 第
1
页
上一页
1
下一页
相关的教程:
XML
序列化
反序列化
收藏此教程
Currently.-0.05/5
-5
-4
-3
-2
-1
0
1
2
3
4
5
当前平均分:
0.0
(
0
次打分)
-5
-4
-3
-2
-1
0
1
2
3
4
5
推荐阅读
Asp.Net对Xml文件的操作
ASP.NET XML高级编程:C#篇
XML初学者必读
十分钟内学会:将HTML格式化为合法的XML
XML轻松学习手册
操作XML
使用xml绑定实现分页功能
XML轻松学习手册
使用XmlSerializer序列化和反序列化一个类
使用XmlSerializer序列化和反序列化一个类
添加评论
评论主题
您的大名
您的评论
验证码
评论列表
ASP.NET论坛
|
网站帮助
|
加入收藏
知识库搜索:
用户信息
欢迎您,游客。
登录
|
注册
为什么要注册?
马上加入GotAspx,建立自己的知识库,与大家分享您的知识库,还可获得丰厚积分奖励!
本类热门
从零开始学ASP.NET(基础篇)
ASP.NET 2.0轻松实现数据库应用开发
ASP.NET 程序中常用的三十三种代码
GridView 72般绝技(一)
对初学者的建议:ASP.NET技术的学习顺序
单点登录在ASP.NET上的简单实现
ASP.NET数据库编程快速入门之技术慨述
ASP.NET 2.0中构造个性化网页
本类最新
介绍SubSonic【转】
ASP.NET访问XML的例子
WEB开发者版本级别
基于.NET2.0的System.Net.Mail发送邮件Demo
Asp.Net 文件操作基类(读取,删除,批量拷贝,批量删除,写入)
c#生成与 追加xml
一个复杂的Eval()绑定
将服务器上的一个.doc文档另存为到客户端