gpt4 book ai didi

c# - System.Configuration.ConfigurationErrorsException - 无法识别的元素 'setting'

转载 作者:行者123 更新时间:2023-11-30 16:09:37 26 4
gpt4 key购买 nike

Unrecognized element 'setting'. (C:\Dev\DOT.NET\AutoPay\ibeam.config line 15)

AppInfo.Sections.Get("AutoPay.Common.Credentials") 'AppInfo.Sections.Get("AutoPay.Common.Credentials")' threw an exception of type 'System.Configuration.ConfigurationErrorsException' System.Configuration.ConfigurationSection {System.Configuration.ConfigurationErrorsException}

我的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="AppInfo" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="AutoPay.Common.Credentials"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser"
requirePermission="false" />
</sectionGroup>
</configSections>

<AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String"><value>********</value></setting>
<setting name="Password" serializeAs="String"><value>********</value></setting>
<setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
</AutoPay.Common.Credentials>
</AppInfo>
</configuration>

基(抽象)类:

public abstract class BaseConfigInfo : ConfigurationSectionGroup
{
protected Configuration Configuration;
protected ConfigurationSectionGroup AppInfo;
protected ClientSettingsSection Credentials;

protected BaseConfigInfo(string configFile)
{
var fileMap = new ConfigurationFileMap(configFile);
Configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
AppInfo = Configuration.GetSectionGroup("AppInfo");

// THIS IS WHERE THE ERROR POPS //
Credentials = (ClientSettingsSection)AppInfo.Sections.Get("AutoPay.Common.Credentials");
}

protected string GetCredentialsString(string kvpName)
{
var setting = Credentials.Settings.Get(kvpName);
return setting.Value.ValueXml.InnerText;
}

public string UserName { get { return GetCredentialsString("UserName"); } }
public string Password { get { return GetCredentialsString("Password"); } }
public string ServiceUrl { get { return GetCredentialsString("ServiceUrl"); } }
}

示例具体类:

public class ConfigInfoIbeam : BaseConfigInfo
{
public ConfigInfoIbeam() : base(ConfigurationManager.AppSettings["ConfigInfoIbeam"]) { }
}

我知道我在这里遗漏了一些简单的东西。我在 .NET 4 中运行。当我尝试用谷歌搜索此错误时,没有显示任何有用信息。有什么想法吗?

最佳答案

我发现了问题。在我的配置中,我有这个:

  <AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String"><value>********</value></setting>
<setting name="Password" serializeAs="String"><value>********</value></setting>
<setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
</AutoPay.Common.Credentials>
</AppInfo>

我把它改成:

  <AppInfo>
<AutoPay.Common.Credentials>
<setting name="UserName" serializeAs="String">
<value>********</value>
</setting>
<setting name="Password" serializeAs="String">
<value>********</value>
</setting>
<setting name="ServiceUrl" serializeAs="String">
<value>********</value>
</setting>
</AutoPay.Common.Credentials>
</AppInfo>

不要问我为什么底层的xml阅读器无法区分两者。不过,问题解决了!呜呜……

关于c# - System.Configuration.ConfigurationErrorsException - 无法识别的元素 'setting',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27279384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com