내 web.config 파일에 사전 객체를 저장하려면 어떻게합니까?
내 웹 구성 파일에 간단한 키 / 값 문자열 사전을 저장하고 싶습니다. Visual Studio를 사용하면 문자열 컬렉션 (아래 샘플 참조)을 쉽게 저장할 수 있지만 사전 컬렉션으로 수행하는 방법을 잘 모르겠습니다.
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>value1</string>
<string>value2</string>
<string>value2</string>
</ArrayOfString>
바퀴를 재발 명하는 이유는 무엇입니까? 있는 appSettings 섹션은 설정 파일의 데이터 사전과 같은 저장 정확히 목적을 위해 설계되었습니다.
AppSettings 섹션에 너무 많은 데이터를 넣지 않으려면 다음과 같이 관련 값을 고유 한 섹션으로 그룹화 할 수 있습니다.
<configuration>
<configSections>
<section
name="MyDictionary"
type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<MyDictionary>
<add key="name1" value="value1" />
<add key="name2" value="value2" />
<add key="name3" value="value3" />
<add key="name4" value="value4" />
</MyDictionary>
</configuration>
다음을 사용하여이 컬렉션의 요소에 액세스 할 수 있습니다.
using System.Collections.Specialized;
using System.Configuration;
public string GetName1()
{
NameValueCollection section =
(NameValueCollection)ConfigurationManager.GetSection("MyDictionary");
return section["name1"];
}
Juliet의 대답은 정답이지만 참고 로 다음과 같이 .config
설정하여 외부 파일 에 추가 구성을 넣을 수도 web.config
있습니다.
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- blah blah the default stuff here -->
<!-- here, add your custom section -->
<section name="DocTabMap" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<!-- your custom section, but referenced in another file -->
<DocTabMap file="CustomDocTabs.config" />
<!-- etc, remainder of default web.config is here -->
</configuration>
그러면 다음과 같이 CustomDocTabs.config
보입니다.
<?xml version="1.0"?>
<DocTabMap>
<add key="A" value="1" />
<add key="B" value="2" />
<add key="C" value="3" />
<add key="D" value="4" />
</DocTabMap>
Now you can access it in code via:
NameValueCollection DocTabMap = ConfigurationManager.GetSection("DocTabMap") as NameValueCollection;
DocTabMap["A"] // == "B"
You would need to implement a custom section (See Configuration Section Designer).
What you really want... is something close to this:
<MyDictionary>
<add name="Something1" value="something else"/>
<add name="Something2" value="something else"/>
<add name="Something3" value="something else"/>
</MyDictionary>
Where the XmlAttribute "name" is a Key which it won't allow to have more than one in the code behind. At the same time, make sure that the Collection MyDictionary is also a Dictionary.
You can do all of this with this tool and fill the gap as needed.
In application settings we can use System.Collection.Specilized.StringCollection
<X.Properties.Settings>
<setting name="ElementsList" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>Element1</string>
<string>Element2</string>
</ArrayOfString>
</value>
</setting>
</X.Properties.Settings>
Access to list:
var element = Settings.Default.ElementsList[index]
I'm not sure how to store a Dictionary directly but you could easily use an array of strings to store a dictionary. For every key, value pair you save out the key as the first string and the value as the second. Then when rebuilding the dictionary you can undo this encoding.
static Dictionary<string,string> ArrayToDictionary(string[] data) {
var map = new Dictionary<string,string>();
for ( var i= 0; i < data.Length; i+=2 ) {
map.Add(data[i], data[i+1]);
}
return map;
}
참고URL : https://stackoverflow.com/questions/338242/how-do-i-store-a-dictionary-object-in-my-web-config-file
'Nice programing' 카테고리의 다른 글
Spring Boot 애플리케이션의 환경 별 application.properties 파일 (0) | 2020.11.26 |
---|---|
Swift에서 throw와 rethrows의 차이점은 무엇입니까? (0) | 2020.11.26 |
Android에서 GUID를 얻는 방법은 무엇입니까? (0) | 2020.11.26 |
MSTest : 테스트가로드되지 않았거나 선택한 테스트가 비활성화 되었기 때문에 테스트가 실행되지 않습니다. (0) | 2020.11.26 |
PuTTY를 사용하여 Windows에서 Linux에서 명령 실행 자동화 (0) | 2020.11.26 |