您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

2026-04-12 12:132阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计618个文字,预计阅读时间需要3分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

csharp基类:使用 System 命名空间;使用 System.Collections.Generic 命名空间;使用 System.Linq 命名空间;使用 System.Text 命名空间;命名空间 DeserializeTest{ public class SettingsBase { private string m_fileName; public string FileName { get { return m_fileName; } set { m_fileName=value; } } }}

基类:

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; } } } }

派生类:

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; } } } }

主函数调用测试代码为:

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文件:

<?xml version="1.0" encoding="utf-8"?> <SettingsBase> <FileName>WorldWind.xml</FileName> </SettingsBase>

派生类的XML文件:

<?xml version="1.0" encoding="utf-8"?> <WorldWindSettings> <FileName>WorldWind.xml</FileName> <ProxyUrl>www.baidu.com</ProxyUrl> </WorldWindSettings>

源码下载:DeserializeTest.rar 提取码:djpe

总结:将xml文件反序列化为类的实例的时候,只要xml文件的根节点的名字与被反序列化的类的名字一致即可。当然了,反序列化成功后,用基类的实例引用去指向反序列化后的派生类的实例也是没问题的。

其它注意事项:

如果在一个类中有静态的成员变量,则在该类调用构造函数实例化之前,会首先实例化静态的成员变量。

以上就是本次介绍的全部知识点内容,感谢大家的学习和对易盾网络的支持。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

本文共计618个文字,预计阅读时间需要3分钟。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。

csharp基类:使用 System 命名空间;使用 System.Collections.Generic 命名空间;使用 System.Linq 命名空间;使用 System.Text 命名空间;命名空间 DeserializeTest{ public class SettingsBase { private string m_fileName; public string FileName { get { return m_fileName; } set { m_fileName=value; } } }}

基类:

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; } } } }

派生类:

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; } } } }

主函数调用测试代码为:

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文件:

<?xml version="1.0" encoding="utf-8"?> <SettingsBase> <FileName>WorldWind.xml</FileName> </SettingsBase>

派生类的XML文件:

<?xml version="1.0" encoding="utf-8"?> <WorldWindSettings> <FileName>WorldWind.xml</FileName> <ProxyUrl>www.baidu.com</ProxyUrl> </WorldWindSettings>

源码下载:DeserializeTest.rar 提取码:djpe

总结:将xml文件反序列化为类的实例的时候,只要xml文件的根节点的名字与被反序列化的类的名字一致即可。当然了,反序列化成功后,用基类的实例引用去指向反序列化后的派生类的实例也是没问题的。

其它注意事项:

如果在一个类中有静态的成员变量,则在该类调用构造函数实例化之前,会首先实例化静态的成员变量。

以上就是本次介绍的全部知识点内容,感谢大家的学习和对易盾网络的支持。

您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。