zoukankan      html  css  js  c++  java
  • C#中将xml文件反序列化为实例时采用基类还是派生类的问题

    基类:

    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:MyProjectDeserializeTestDeserializeTestinDebugGobalConfigSettingsBase.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:MyProjectDeserializeTestDeserializeTestinDebugGobalConfigWorldWind.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    提取码:pkya 

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

  • 相关阅读:
    android: 在android studio中使用retrolambda的步骤
    Shiro基础知识03----shiro授权(编程式授权),Permission详解,授权流程(zz)
    [Android] 关于Android的问号?和@符号的用法
    20170506 《摔跤吧,爸爸》观影笔记
    Android关于log日志,华为不输出log.v,log.d(zz)
    阅读日志:协鑫一年的多晶硅料产能能够生产做少装机容量的组件?
    《周鸿祎自述》读书笔记
    使用promise 和 generator来管理工作流
    读书笔记,《刻意练习》,第三章,心理表征
    读书笔记,《刻意练习》,第四章,黄金标准
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/11839330.html
Copyright © 2011-2022 走看看