RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#中如何将xml文件反序列化为实例时采用基类还是派生类-创新互联

这篇文章主要介绍C#中如何将xml文件反序列化为实例时采用基类还是派生类,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及航空箱等,在成都网站建设成都全网营销推广、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。

基类:

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



 WorldWind.xml

派生类的XML文件:



 WorldWind.xml
 www.baidu.com

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

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

其它注意事项:

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

以上是“C#中如何将xml文件反序列化为实例时采用基类还是派生类”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:C#中如何将xml文件反序列化为实例时采用基类还是派生类-创新互联
网站URL:http://sczitong.cn/article/dgjdco.html