有没有办法将部分添加到不会被ConfigurationManager解析的.config文件中

时间:2021-10-01 20:08:23

I don't want to create separate configuration file for my app but store the data in the web.config.

我不想为我的应用程序创建单独的配置文件,而是将数据存储在web.config中。

I just want to put some XML there and manually parse it because current implementation of ConfigurationManager isn't appropriate for my case.

我只想在那里放一些XML并手动解析它,因为ConfigurationManager的当前实现不适合我的情况。

However, without dummy classes and properties I can't add my XML there without getting Configuration Error: Parser Error Message: Unrecognized element XXXXX. Even if I create dummy configuration classes I can't fix this error in all cases...

但是,如果没有虚拟类和属性,我无法在没有配置错误的情况下添加我的XML:分析器错误消息:无法识别的元素XXXXX。即使我创建虚拟配置类,我无法在所有情况下修复此错误...

Is there any way to mark my XML not to be parsed so that I can use System.XML to manually get the data.

有没有办法标记我的XML不被解析,以便我可以使用System.XML手动获取数据。

Thx.

3 个解决方案

#1


Use System.Configuration.IgnoreSectionHandler.

Example:

<configuration>
  <configSections>
    <section 
        name="myCustomSection" 
        type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
        allowLocation="false" />
  </configSections>
...
  <myCustomSection>
     ...
  </myCustomSection>
...
</configuration>

#2


I would suggest creating a custom figuration section if you want to store the data in your .configs

如果您想将数据存储在.configs中,我建议您创建一个自定义配置部分

www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx

#3


I don't think you can do this - you need to conform to the XML schema for configuration files if you play around in the *.config files - and this means, you need to have your sections according to the specified schema, and if that's the case, ConfigurationManager will be able to parse them (and will do so).

我不认为你可以这样做 - 如果你在* .config文件中玩游戏,你需要符合配置文件的XML模式 - 这意味着你需要根据指定的模式包含你的部分,如果在这种情况下,ConfigurationManager将能够解析它们(并将这样做)。

If you need extra config information that doesn't conform to the standard .NET config schema, you'll have to use extra, separate config files - I don't see any other way, sorry.

如果您需要不符合标准.NET配置架构的额外配置信息,则必须使用额外的单独配置文件 - 我没有看到任何其他方式,抱歉。

Marc

#1


Use System.Configuration.IgnoreSectionHandler.

Example:

<configuration>
  <configSections>
    <section 
        name="myCustomSection" 
        type="System.Configuration.IgnoreSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
        allowLocation="false" />
  </configSections>
...
  <myCustomSection>
     ...
  </myCustomSection>
...
</configuration>

#2


I would suggest creating a custom figuration section if you want to store the data in your .configs

如果您想将数据存储在.configs中,我建议您创建一个自定义配置部分

www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx

#3


I don't think you can do this - you need to conform to the XML schema for configuration files if you play around in the *.config files - and this means, you need to have your sections according to the specified schema, and if that's the case, ConfigurationManager will be able to parse them (and will do so).

我不认为你可以这样做 - 如果你在* .config文件中玩游戏,你需要符合配置文件的XML模式 - 这意味着你需要根据指定的模式包含你的部分,如果在这种情况下,ConfigurationManager将能够解析它们(并将这样做)。

If you need extra config information that doesn't conform to the standard .NET config schema, you'll have to use extra, separate config files - I don't see any other way, sorry.

如果您需要不符合标准.NET配置架构的额外配置信息,则必须使用额外的单独配置文件 - 我没有看到任何其他方式,抱歉。

Marc