-
C#教程之C#实现复杂XML的序列化与反序列化
本文以一个实例的形式讲述了C#实现复杂XML的序列化与反序列化的方法。分享给大家供大家参考。具体方法如下:
已知.xml(再此命名default.xml)文件,请将其反序列化到一个实例对象。
Default.XML文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<? xml version = "1.0" encoding = "utf-8" ?> < config > < rules > < rule name = "namea" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "nameb" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "namec" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > < rule name = "named" > < params > < item name = "p" type = "get" pattern = "(ios|android|)" /> < item name = "cid" type = "get" pattern = "(ios|android|)" /> < item name = "type" type = "get" pattern = "(ios|android|)" /> < item name = "sort" type = "get" pattern = "(ios|android|)" /> < item name = "action" type = "post" pattern = "(ios|android|)" /> < item name = "userAgent" type = "header" pattern = "(ios|android|)" /> </ params > </ rule > </ rules > </ config > |
C#示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace 多层级反序列 { [XmlType(TypeName = "config" )] public class XmlConfig { [XmlArray( "rules" )] public List<rule> rules { get ; set ; } } [XmlType(TypeName = "rule" )] public class rule { [XmlAttribute] public string name { get ; set ; } [XmlArray( "params" )] public List<paramsItem> @ params { get ; set ; } } [XmlType(TypeName = "item" )] public class paramsItem { [XmlAttribute] public string name { get ; set ; } [XmlAttribute] public string type { get ; set ; } [XmlAttribute] public string pattern { get ; set ; } } class Program { static void Main( string [] args) { /* XmlConfig xml1 = DeserializeFromXml<XmlConfig>("default.xml"); XmlConfig xml = new XmlConfig(); SerializeToXml<XmlConfig>("default.xml", xml); */ XmlConfig xml1 = DeserializeFromXml<XmlConfig>( "default.xml" ); } /// <summary> /// XML序列化某一类型到指定的文件 /// </summary> /// <param name="filePath"></param> /// <param name="obj"></param> /// <param name="type"></param> public static void SerializeToXml<T>( string filePath, T obj) { try { using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath)) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof (T)); xs.Serialize(writer, obj); } } catch (Exception ex) { } } /// <summary> /// 从某一XML文件反序列化到某一类型 /// </summary> /// <param name="filePath">待反序列化的XML文件名称</param> /// <param name="type">反序列化出的</param> /// <returns></returns> public static T DeserializeFromXml<T>( string filePath) { try { if (!System.IO.File.Exists(filePath)) throw new ArgumentNullException(filePath + " not Exists" ); using (System.IO.StreamReader reader = new System.IO.StreamReader(filePath)) { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof (T)); T ret = (T)xs.Deserialize(reader); return ret; } } catch (Exception ex) { return default (T); } } } } |
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比