1

1 つの入力ファイルから 1 つの辞書を作成する方法を 1 つ見つけましたが、複数の入力ファイルを考慮していません。次のステップに進むために、追加の入力ファイルを既存のハッシュに追加し、別のことを試みたいと考えています。このコードは、達成したいものに多少近いですが、まだ構文がありません。

        foreach (string file in filePaths)
        {
            XDocument xdoc = XDocument.Load(file);

            allDict = (from m in xdoc.Descendants("msg")
                        .ToDictionary(m => m.Element("msgId").Value,
                                      m => new msg
                                      {
                                        msgId = m.Element("msgId").Value,
                                        msgType = m.Element("msgType").Value,
                                        name = m.Element("name").Value
                                      }
            )
        }
4

1 に答える 1

0

「マスター」オブジェクトを作成し、各ファイルから取得した値を追加します。

Dictionary<string, msg> masterDictionary = new Dictionary<string, mgs>();

foreach (string file in filePath)
{
    XDocument xdoc = XDocument.Load(file);
    Dictionary<string, msg> fileDictionary = xdoc
        .Descendants("msg")
        .ToDictionary(m => m.Element("msgId").Value,
                      m => new msg
                           {
                               msgId = m.Element("msgId").Value,
                               msgType = m.Element("msgType").Value,
                               name = m.Element("name").Value
                           });

    //now insert your new values into the master
    foreach(var newValue in fileDictionary)
        masterDictionary.Add(newValue.Key, newValue.Value);
}
于 2013-10-17T01:02:13.617 に答える