使用 XSD 生成的类反序列化 web.config 自定义部分

发布于 2024-11-16 14:11:58 字数 266 浏览 3 评论 0原文

我在 Web.Config 文件中有一个自定义部分,并通过 configSource 引用另一个 .config 文件。我已使用 XSD 为我的架构生成了 .cs 文件。如何使用我生成的类来反序列化配置。当我尝试使用 .GetSection() 方法加载 configsection 时,它给出了该类应从 ConfigurationSection 实现的错误。由于我使用 xsd 生成了这些类,因此它们不是从 ConfigurationSection 或 ConfigurationElement 继承的。提前致谢!!

I Have a custom section in Web.Config file and referring another .config file through configSource. I have generated .cs file using the XSD for my schema. How to use my generated classes to deserialize the config. When I try to load the configsection using .GetSection() method, it gives me error that the class should implement from ConfigurationSection. Since I have generated the classes using xsd, they were not inherited from ConfigurationSection or ConfigurationElement. Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紙鸢 2024-11-23 14:11:58

我已经实现了 ConfigurationSection 中的类来获取文件名,并使用 XMLSerializer 使用 XSD 工具生成的类进行反序列化。这是示例:

namespace mycontrol
{
      public class ConfigurationSection : System.Configuration.ConfigurationSection
        {
           //Configuration is the type generated by XSD for my schema element Configuration

            private static Configuration _config; 

            /// <summary>
            /// static method to load config section and deserialize config
            /// </summary>
            /// <returns></returns>
            private static Configuration GetConfig()
            {
                if (_config == null)
                {
                    ConfigurationSection configSection = (ConfigurationSection)ConfigurationManager.GetSection("SearchControlsConfig") as ConfigurationSection;
                    if (!string.IsNullOrEmpty(configSection.ExternalConfigSource))
                    {
                        string strFilePath = configSection.ExternalConfigSource;
                        if (!File.Exists(strFilePath))
                        {
                            strFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strFilePath);
                            if (!File.Exists(strFilePath))
                                return null;
                        }

                        using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
                        {
                            XmlSerializer config = new XmlSerializer(typeof(Configuration));
                            _config = (Configuration)config.Deserialize(fs);
                        }
                    }

                }

                return _config;
            }


            /// <summary>
            /// Attribute to specify config source file in custom config section
            /// </summary>
            [ConfigurationProperty("externalConfigSource", DefaultValue = "", IsRequired = false)]
            public string ExternalConfigSource
            {
                get
                {
                    return this["externalConfigSource"] as string;
                }
            }
        }
}

在指定自定义配置时,我无法使用 configSource 属性作为我的属性,因为它是由 ConfigurationManager 保留的。

I have implemented the class from ConfigurationSection to get the file name and using XMLSerializer to Deserialize using the classes generated by XSD tool. Here is sample:

namespace mycontrol
{
      public class ConfigurationSection : System.Configuration.ConfigurationSection
        {
           //Configuration is the type generated by XSD for my schema element Configuration

            private static Configuration _config; 

            /// <summary>
            /// static method to load config section and deserialize config
            /// </summary>
            /// <returns></returns>
            private static Configuration GetConfig()
            {
                if (_config == null)
                {
                    ConfigurationSection configSection = (ConfigurationSection)ConfigurationManager.GetSection("SearchControlsConfig") as ConfigurationSection;
                    if (!string.IsNullOrEmpty(configSection.ExternalConfigSource))
                    {
                        string strFilePath = configSection.ExternalConfigSource;
                        if (!File.Exists(strFilePath))
                        {
                            strFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strFilePath);
                            if (!File.Exists(strFilePath))
                                return null;
                        }

                        using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read))
                        {
                            XmlSerializer config = new XmlSerializer(typeof(Configuration));
                            _config = (Configuration)config.Deserialize(fs);
                        }
                    }

                }

                return _config;
            }


            /// <summary>
            /// Attribute to specify config source file in custom config section
            /// </summary>
            [ConfigurationProperty("externalConfigSource", DefaultValue = "", IsRequired = false)]
            public string ExternalConfigSource
            {
                get
                {
                    return this["externalConfigSource"] as string;
                }
            }
        }
}

I cannot use the configSource attribute as my property while specifying the custom config, because it is reserved by ConfigurationManager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文