나는 Jon Skeet에 동의합니다. 단일 사람, 예를 들어 <Person>
엔티티에서 동일한 사람과 관련된 정보를 랩핑하는 것은보다 구조화 된 접근 방식입니다. 그리고 각주의 이름이나 ID를 알려 주어야합니다. 그렇지 않으면 그룹이 임의로 나타납니다.
<states>
<state Name="Florida">
<Person Name="a1">
<Address>a2</Address>
</Person>
<Person Name="b1">
<Address>b2</Address>
</Person>
<Person Name="c1">
<Address>c2</Address>
</Person>
</state>
<state Name="New York">
<Person Name="aa1">
<Address>aa2</Address>
</Person>
<Person Name="bb1">
<Address>bb2</Address>
</Person>
</state>
</states>
다음은 2 단계 사전을 구성하는 방법입니다. 외부 사전은주의 이름으로 키가되며, 해당 주에 거주하는 사람이 들어있는 사전에 의해 평가됩니다. 내부 사전은 사람의 이름을 입력하고 주소로 평가됩니다. 따라서
XDocument document = XDocument.Parse(@"
<states>
<state Name=""Florida"">
<Person Name=""a1"">
<Address>a2</Address>
</Person>
<Person Name=""b1"">
<Address>b2</Address>
</Person>
<Person Name=""c1"">
<Address>c2</Address>
</Person>
</state>
<state Name=""New York"">
<Person Name=""aa1"">
<Address>aa2</Address>
</Person>
<Person Name=""bb1"">
<Address>bb2</Address>
</Person>
</state>
</states>");
IDictionary<string, Dictionary<string, string>> dictionary =
document.Root.Elements("state").ToDictionary(
state => state.Attribute("Name").Value,
state => state.Elements("Person").ToDictionary(
person => person.Attribute("Name").Value,
person => person.Element("Address").Value));
// To get address of "a1" living in Florida:
string addr1 = dictionary["Florida"]["a1"]; // gives "a2"
// To get address of "bb1" living in New York:
string addr2 = dictionary["New York"]["bb1"]; // gives "bb2"
, 당신은
dictionary[C][X]
에 접근 할 필요가, 도시
C
에 살고있는 사람
X
의 주소를 얻을 수 있습니다. (키와 새 이름)을 다시 추가 한 후
먼저 (내부 사전에서) 자신의 항목을 제거하여 사람의 이름을 변경할 수 있으며, :
편집 : 회신 언급합니다. 예를 들어
는 이름을 "aa1"
"aa2"
에 변경하기 : 자주 키를 변경해야하는 경우
말했다
Dictionary<string, string> innerDictionary = dictionary["New York"];
string address = innerDictionary["aa1"];
innerDictionary.Remove("aa1");
innerDictionary.Add("xx1", address);
의 Dictionary<TKey, TValue>
최고의 구조되지 않을 수 있습니다.
문제가 발생했거나 시도하지 않으셨습니까? –
XML 파일 *이 이렇게 구성되어 있습니까? "주문 후 첫 번째 주소 요소가 해당 이름의 주소 인"것에 의존하여 주문에 특화되는 것은 그리 좋지 않습니다. –
당신은 C# 3.0과 C# 4.0이지만 아직 Hashtable을 원한다. 일반적으로 System.Collections.Generic.Dictionary를 대신 살펴보십시오. 2.0 + –