-
C#教程之C#中将xml文件反序列化为实例时采用基类还是派生类的知识点讨论
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
基类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DeserializeTest { public class SettingsBase { private string m_fileName; public string FileName { get { return m_fileName; } set { m_fileName = value; } } } } |
派生类:
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
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DeserializeTest { public class WorldWindSettings : SettingsBase { public WorldWindSettings() : base () { } private string m_proxyUrl = "" ; public string ProxyUrl { get { return m_proxyUrl; } set { this .m_proxyUrl = value; } } } } |
主函数调用测试代码为:
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
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml.Serialization; namespace DeserializeTest { class Program { static void Main( string [] args) { //测试1:测试将xml文件反序列化为基类实例。测试通过。只要xml文件的根节点的名字与被反序列化的类的名字一致即可 string fileNameBase = @"D:\MyProject\DeserializeTest\DeserializeTest\bin\Debug\GobalConfig\SettingsBase.xml" ; SettingsBase settingsBase; XmlSerializer serBase = new XmlSerializer( typeof (SettingsBase)); using (TextReader trBase = new StreamReader(fileNameBase)) { settingsBase = (SettingsBase)serBase.Deserialize(trBase); settingsBase.FileName = fileNameBase; } //测试2:测试将xml文件反序列化为子类实例。测试通过。只要xml文件的根节点的名字与被反序列化的类的名字一致即可。当然了,用基类的实例引用去指向反序列化后的派生类的实例也是没问题的。 string fileName = @"D:\MyProject\DeserializeTest\DeserializeTest\bin\Debug\GobalConfig\WorldWind.xml" ; SettingsBase settings; //当前了此处定义为WorldWindSettings settings;也没问题 Type type = typeof (WorldWindSettings); //因为xml文件的根节点名称是WorldWindSettings,此处只能为WorldWindSettings,而不能为SettingsBase XmlSerializer ser = new XmlSerializer(type); using (TextReader tr = new StreamReader(fileName)) { //settings = (WorldWindSettings)ser.Deserialize(tr);//这两句代码都可以通过! settings = (SettingsBase)ser.Deserialize(tr); settings.FileName = fileName; } System.Console.WriteLine( "Hello" ); } } } |
基类的XML文件:
1
2
3
4
|
<?xml version= "1.0" encoding= "utf-8" ?> <SettingsBase> <FileName>WorldWind.xml</FileName> </SettingsBase> |
派生类的XML文件:
1
2
3
4
5
|
<?xml version= "1.0" encoding= "utf-8" ?> <WorldWindSettings> <FileName>WorldWind.xml</FileName> <ProxyUrl>www.baidu.com</ProxyUrl> </WorldWindSettings> |
源码下载:DeserializeTest.rar 提取码:djpe
总结:将xml文件反序列化为类的实例的时候,只要xml文件的根节点的名字与被反序列化的类的名字一致即可。当然了,反序列化成功后,用基类的实例引用去指向反序列化后的派生类的实例也是没问题的。
其它注意事项:
如果在一个类中有静态的成员变量,则在该类调用构造函数实例化之前,会首先实例化静态的成员变量。
以上就是本次介绍的全部知识点内容,感谢大家的学习和对脚本之家的支持。
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日