更新:自定义配置非常糟糕! >。<虽然我学到了一些关于XML的东西;显然,将CDATA存储在一个元素中是不可能的,所以我已经改写了这个问题以适应修改后的结构。为了清楚起见,请问我任何问题。我已经在这个问题上工作了大约一个月,并且一直在遇到各种各样的问题。
我在下面引用的文章中一直关注MSDN示例,但我在将这个概念应用到我的案例时遇到了麻烦。我的项目是VS2010中的VB.NET 4.0 Web应用程序,但C#中的答案也没问题(只要解决方案可以在VB中运行)。 :)以下是相关代码:
拨打:
Dim conf As ApiSection = CType(ConfigurationManager.GetSection("remoteServiceApi/site"), ApiSection)
异常(ConfigurationErrorsException)
The element <site> may only appear once in this section.
请注意以下相关代码......
模式:
<xs:element name="ApiSection">
<xs:complexType>
<xs:sequence>
<xs:element name="site" id="environment" nillable="false" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="environment" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Production"></xs:enumeration>
<xs:enumeration value="Sandbox"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="APIKey" type="xs:string" use="required" />
<xs:attribute name="APIUsername" type="xs:string" use="required" />
<xs:attribute name="APIPassword" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
即使我在web.config文件的模式集中添加并激活了上述内容,但它似乎并没有真正起作用。 :(这可能是问题吗?
的Web.Config :
<configSections>
<sectionGroup name="remoteServiceApi">
<section name="site" type="RemoteServiceApi.ApiSection" />
</sectionGroup>
</configSections>
<remoteServiceApi environment="Sandbox">
<site environment="Sandbox"
APIKey="reallyLongHashKeyDev"
APIUsername="samplejoe"
APIPassword="joespass" />
<site environment="Production"
APIKey="reallyLongHashKeyProd"
APIUsername="samplejoe"
APIPassword="joespass" />
</remoteServiceApi>
类文件:
Imports System.Configuration
Namespace RemoteServiceApi
Public Class ApiSection
Inherits ConfigurationSection
<ConfigurationProperty("site", IsRequired:=True)> _
Public Property site() As SiteElement
Get
Return CType(Me("site"), SiteElement)
End Get
Set(value As SiteElement)
Me("site") = value
End Set
End Property
End Class
Public Class SiteElement
Inherits ConfigurationElement
<ConfigurationProperty("Environment", DefaultValue:="Sandbox", IsRequired:=True)> _
Public Property Environment() As String
Get
Return CStr(Me("Environment"))
End Get
Set(value As String)
Me("Environment") = value
End Set
End Property
<ConfigurationProperty("APIKey", IsRequired:=True)> _
Public ReadOnly Property APIKey() As String
Get
Return CStr(Me("APIKey"))
End Get
End Property
<ConfigurationProperty("APIUsername", IsRequired:=True)> _
Public ReadOnly Property APIUsername() As String
Get
Return CStr(Me("APIUsername"))
End Get
End Property
<ConfigurationProperty("APIPassword", IsRequired:=True)> _
Public ReadOnly Property APIPassword() As String
Get
Return CStr(Me("APIPassword"))
End Get
End Property
End Class
Public Enum Environment
Production
Sandbox
End Enum
End Namespace
参考资料我用作指南:
答案 0 :(得分:1)
您需要将Site
元素设为一个集合(基于您发布的Web.config)。尝试这样的事情(在C#中,抱歉):
[ConfigurationCollection(typeof(SiteElement)[
public class SiteElementCollection : ConfigurationElementCollection
public SiteElement this[string name]
{
get
{
return (SiteElement)base.BaseGet(name);
}
}
public SiteElement this[int index]
{
get
{
return (SiteElement)base.BaseGet(index);
}
}
public override ConfigurationElement CreateNewElement()
{
return new SiteElement();
}
public override object GetElementKey(ConfigurationElement element)
{
return ((SiteElement)element).AddressKey;
}
此类将包装SiteElement
,允许您拥有该元素的多个实例。
接下来的部分我并不是100%肯定,因为我从未在该部分下使用ConfigurationElementCollection
,但您需要引用该集合。
作为一个例子(它不太适合你想要做的,但希望适合),假设你在部分组下有一个部分名称Sites
。你可以这样做:
public SiteElementCollection Sites
{
get
{
return (SiteElementCollection)base["site"];
}
}
我希望这至少可以为你提供一个方向。这里的关键是当你需要拥有给定元素的多个实例时使用ConfigurationElementCollection。