簡體   English   中英

在Web.Config中存儲字符串的可變集合?

[英]Storing a variable collection of strings in Web.Config?

我想在web.config中存儲字符串集合。 該集合的大小會隨時間而變化。 我希望能夠將集合中的所有字符串拉入數組或代碼集合中。 (.Net 4,asp.net)即

<customCodes>
<VendorCode vendorName="Name1" code="1234567891234567891324567987ddd" isActive="true"/>
<VendorCode vendorName="Name2" code="1sadfsadf1234567891324567987ddd"  isActive="true" />
<VendorCode vendorName="Name3" code="123456789dfadfdsaf3324567987d32"  isActive="true"/>
</customCodes>

我可以將appsettings與所有字符串都使用一個值,但出於組織原因,我想將其分開。

任何代碼段或示例將不勝感激。

抱歉,這是第一次發布而沒有代碼示例。 不使用鍵/值對會使事情復雜一些。 我現在收到一條消息,指出“ 部分中不能有重復的元素

提亞

您可以通過創建自定義配置節處理程序來做到這一點:

http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

對於集合,請查看ConfigurationElementCollection

http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx

在第二個鏈接之后,有一些您需要用來解決問題的代碼。 只需更改一些標簽並使用“添加”而不是“ VendorCode”即可:-)

http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

鏈接http://dotnetslackers.com/articles/customconfiguration/Custom_Configuration_Collections.aspx對完成我想做的事情很有幫助。 我確實必須適應使用“添加”標簽,而不是我最初想要的自定義標簽。

感謝Dirk的幫助。

Ĵ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM