C# System.Configuration.ConfigXmlElement中的强制转换异常无效
我正在尝试使用下面的代码访问我的“web.config”文件的一个部分C# System.Configuration.ConfigXmlElement中的强制转换异常无效,c#,.net,hashtable,config,C#,.net,Hashtable,Config,我正在尝试使用下面的代码访问我的“web.config”文件的一个部分 public static string XMLCheck { get { var section = (Hashtable)ConfigurationManager.GetSection("Default.Framework"); return (string)section["ConnectionString"]; }
public static string XMLCheck
{
get
{
var section = (Hashtable)ConfigurationManager.GetSection("Default.Framework");
return (string)section["ConnectionString"];
}
}
但是将execption设置为无法将类型为“System.Configuration.configxmlement”的对象强制转换为类型为“System.Collections.Hashtable”
这里出了什么问题?如何纠正
?
更新
<Resources>
<Resource Uri="resource:Default:CrossDomain" Version="1.0" Id="8ae96c54" IsEnabled="True" Handler="handler:Default:Framework:Resources:Data:Oracle">
<Properties>
<Property Name="ConnectionString" Value="Data Source=TESTDB;User Id=TESTUSR;password=TESTPWD;Persist Security Info=false"/>
</Properties>
</Resource>
</Resources>
验证web.config中的
configSections
条目是否为DictionarySectionHandler
:
<configuration>
<configSections>
<section name="Default.Framework" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<configuration>
它的
类型=“Default.Framework.Configuration.XmlConfigurationSectionHandler
。需要用它更新什么?谢谢如果您的
只是名称-值对的集合,那么更改类型应该是安全的。如果它是自定义XML结构,那么您需要将其作为ConfigXmlElement
实例获取,并将其解析为XML(例如使用XPath)。谢谢。我的共享代码中需要更新什么?您可以粘贴
元素吗?您可以编辑这些值;我们只需要看看结构,你能建议一下吗。我已经在我的帖子中更新了代码。谢谢
public static string XMLCheck
{
get
{
var section = (XmlElement)ConfigurationManager.GetSection("Default.Framework");
var connectionString = section.SelectSingleNode(@"
descendant::Resource[@Uri='resource:Default:CrossDomain']
/Properties
/Property[@Name='ConnectionString']
/@Value");
return connectionString.Value;
}
}