代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

如何使用空字符串将XML文件读取到字典中

  •  4
  • Thorin Oakenshield  · 技术社区  · 14 年前

    我有一个XML文件,比如:

    <root>
      <RowDetails RowName="A" ColumnSize="1">
        <ColumnDetails ColumnName="A1" />
      </RowDetails>
      <RowDetails RowName="B" ColumnSize="2">
        <ColumnDetails ColumnName="B1" />
        <ColumnDetails ColumnName="B2" />
      </RowDetails>
      <RowDetails RowName="C" ColumnSize="3">
        <ColumnDetails ColumnName="C1" />
        <ColumnDetails ColumnName="C2" />
        <ColumnDetails ColumnName="C3" />
      </RowDetails>
    </root>
    

    还有字典,比如:

    Dictionary<String, List<String>>MyDict = new Dictioanary<String, List<String>>();
    

    我正在将XML文件读取到 MyDict 就像:

    XDocument XDoc = XDocument.Load(Application.StartupPath + @"\foo.xml");
    MyDict = XDoc.Descendants("RowDetails").ToDictionary(X => X.Attribute("RowName").Value, 
                                                         X => X.Descendants("ColumnDetails")
                                                               .Select(Y => Y.Attribute("ColumnName").Value).ToList());
    

    现在字典将包含:

    "A"           { "A1" }
    "B"           { "B1", "B2" }
    "C"           { "C1", "C2", "C3" }
    

    但我的问题是我需要所有相同计数的列表。应为空条目添加空字符串,因此预期结果为:

    "A"           { "A1", "", "" }
    "B"           { "B1", "B2", "" }
    "C"           { "C1", "C2", "C3" }
    

    如何修改我的LINQ查询?

    请帮我用LINQ来做这个。

    事先谢谢。

    3 回复  |  直到 14 年前
        1
  •  5
  •   Ani    14 年前

    MyDict


    // The required size is the count of the biggest list
    var sizeRequired = MyDict.Values.Max(l => l.Count);
    
    // Pad each list as necessary
    foreach (var list in MyDict.Values)
        list.AddRange(Enumerable.Repeat(string.Empty, sizeRequired - list.Count));
    

     // The required size is the count of the biggest list
    var sizeRequired = MyDict.Values.Max(l => l.Count);
    
    // Each string key should map to a key in the new dictionary
    // Each List<string> value should map to a new list, padded as necessary.
    var paddedDict = MyDict.ToDictionary
      (
         kvp => kvp.Key,
         kvp => kvp.Value
                   .Concat(Enumerable.Repeat(string.Empty, sizeRequired - kvp.Value.Count))
                   .ToList()
      );
    
        2
  •  2
  •   Merlyn Morgan-Graham    14 年前

    IEnumerable<T>

    public static class IEnumerableExtensions
    {
        public static IEnumerable<TSource> PadIfFewerThan<TSource>(
            this IEnumerable<TSource> items,
            int size, TSource padValue = default(TSource))
        {
            int count = 0;
    
            foreach (var item in items)
            {
                ++count;
                yield return item;
            }
    
            foreach (var index in Enumerable.Range(count, size - count))
                yield return padValue;
        }
    }
    

    var dictionary = doc.Descendants("RowDetails")
        .ToDictionary(
            x => x.Attribute("RowName").Value,
            x => x.Descendants("ColumnDetails")
                .Select(y => y.Attribute("ColumnName").Value)
                .PadIfFewerThan(3, "")
            );
    
    foreach (var entry in dictionary)
        Console.WriteLine(@"""{0}""    {{""{1}""}}",
            entry.Key,
            string.Join(@""",""", entry.Value)
            );
    



    Dictionary<string, List<string>> Dictionary<string, IEnumerable<string>> .ToList()

    var dictionary = doc.Descendants("RowDetails")
        .ToDictionary(
            x => x.Attribute("RowName").Value,
            x => x.Descendants("ColumnDetails")
                .Select(y => y.Attribute("ColumnName").Value)
                .PadIfFewerThan(3)
                .ToList()
            );
    
        3
  •  -1
  •   Remus    14 年前