2010-01-01 102 views
1

我创建了一个复杂的词典。复杂词典打印

mainDict<mainKey, SubDict> 
subDict<subKey, sub1Dict> 
sub1Dict<sub1Key, sub2Dict> 
sub2Dict<sub2Key, sub2Value> 
....... 

如何打印出来这样的(基于VS2005 & .NET 2.0)

mainKey/subKey/sub1Key/sub2Key, sub2Value 

我是否需要转换mainDict到一个列表,并调用join()方法?

谢谢。

+3

嗨,恕我直言好主意,揭示基本类型和类的范围内调节剂(静态,等等),你在一个非常奇怪而复杂的例子中,在你的原始问题中使用“前面”:只在你的第二个问题中。你最后告诉我们'mainDict是:“静态字典<字符串,对象>。我们对各种子键,子字典或sub2Value类型的类型一无所知:我们不知道sub2Dict是否从sub1Dict等进行了分类。我的“心理”能力很弱:) – BillW 2010-01-01 12:40:16

+0

好的。我也同意我原来的帖子不清楚。你们已经帮了我很多。我从你的回复中受益。 - 谢谢。 – 2010-01-01 15:00:12

回答

3
StringBuilder sb = new StringBuilder(); 
foreach (KeyValuePair<mainKey, SubDict> pair1 in mainDict) 
foreach (KeyValuePair<subKey, sub1Dict> pair2 in pair1.Value) 
foreach (KeyValuePair<sub1Key, sub2Dict> pair3 in pair2.Value) 
foreach (KeyValuePair<sub2Key, sub2Value> pair4 in pair3.Value) 
{ 
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
     pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value); 
} 

UPDATE:

StringBuilder sb = new StringBuilder(); 
foreach (KeyValuePair<string, object> pair1 in mainDict) 
foreach (KeyValuePair<string, object> pair2 in (Dictionary<string, object>)pair1.Value) 
foreach (KeyValuePair<string, object> pair3 in (Dictionary<string, object>)pair2.Value) 
foreach (KeyValuePair<string, object> pair4 in (Dictionary<string, object>)pair3.Value) 
{ 
    sb.AppendFormat("{0}/{1}/{2}/{3}, {4}", 
     pair1.Key, pair2.Key, pair3.Key, pair4.Key, pair4.Value); 
} 
+0

嗨Darin,实际上,mainDict是从XSD文件的解析结果生成的。没有关键字可以插入到我的代码中。我将mainDict声明为一个静态字典。恐怕我不能将您的建议(KeyWord内容)作为您的建议。如何扩展StringBuilder循环方法以满足我的要求?谢谢。 – 2010-01-01 10:27:39

+0

看看更新是否有帮助。 – 2010-01-01 10:38:08

+0

sb.AppendFormat(“{0}/{1}/{2}/{3},{4}”, - 不能使用固定的4个变量,项目编号将根据mainDic图层动态变化。意味着mainDict生成动态的,如果XSD文件改变,mainDic也必须改变,而最上面的代码仍然可以处理动态条件。 – 2010-01-01 10:41:50

2

对于这个,看不出比嵌套循环更好的东西 - 遍历mainDict的键,然后是subDict的键,等等。然后,您可以将循环变量和最里面的值呈现为字符串。

2

你可以使用LINQ:

var qry = from pair1 in mainDict 
      from pair2 in pair1.Value 
      from pair3 in pair2.Value 
      from pair4 in pair3.Value 
      select pair1.Key + "/" + pair2.Key + "/" + pair3.Key 
        + "/" + pair4.Key + ", " + pair4.Value; 

foreach(var s in qry) Console.WriteLine(s); 
+0

Hi Marc,vs2005&.net 2.0在我的项目中使用。恐怕我不能使用LINQ技术。 – 2010-01-01 10:09:21

+4

然后,我看到很多嵌套循环在您的未来... – 2010-01-01 10:13:31

+1

有关信息,如果你有VS2008,你仍然可以通过LINQBridge使用LINQ到.NET 2.0的对象:http://www.albahari.com/nutshell /linqbridge.aspx – 2010-01-01 10:15:21

0

最后我想了几分钟在这里找到一个写后一个。在这里,我们去:

 //--------------------------------------------------- 
     // Get a nested Dictionary, then... 
     Console.WriteLine(DictionaryPrint(nestedDictionary)); 
     //--------------------------------------------------- 

     private static string DictionaryPrint(Dictionary<object,object> dictionary, string space = "") 
     { 
      string output = ""; 
      foreach(KeyValuePair<object,object> entry in dictionary) 
      { 
       output += space + entry.Key + ": "; 
       if (entry.Value is Dictionary<object, object>) 
        output += "\n" + DictionaryPrint((Dictionary<object, object>)entry.Value, space + " "); 
       else if (entry.Value is List<object>) 
        output += "\n" + ListPrint((List<object>)entry.Value, space + " "); 
       else 
        output += entry.Value + "\n"; 
      } 
      return output; 
     } 

     private static string ListPrint(List<object> list, string space = "") 
     { 
      string output = ""; 
      foreach (object entry in list) 
      { 
       if (entry is List<object>) 
        output += ListPrint((List<object>)entry, space + " "); 
       else if (entry is Dictionary<object, object>) 
        output += DictionaryPrint((Dictionary<object, object>)entry, space + " "); 
       else 
        output += entry + "\n"; 
      } 
      return output; 
     } 
0

如果你碰巧使用JSON,你可以做逊于

JsonConvert.SerializeObject(yourDict, Formatting.Indented);