简体   繁体   English

尝试将多个XML输入文件加载到iDictionary中<string,Ojbect>

[英]Trying to load multiple XML input files into iDictionary<string,Ojbect>

I found one way to build one dictionary from one input file but it doesn't account for multiple input files. 我发现了一种从一个输入文件构建一个词典的方法,但是它不能解决多个输入文件的问题。 To take my efforts to the next step: I wish to append additional input files into the existing hash and also trying something different this code is somewhat close to what I wish to accomplish but it is still missing syntax. 将我的工作推进到下一步:我希望将其他输入文件追加到现有的哈希中,并且还尝试其他操作,此代码在某种程度上与我希望完成的工作很接近,但是仍然缺少语法。

        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
                                      }
            )
        }

Create a "master" object, then append the values you get from each file. 创建一个“主”对象,然后附加从每个文件获得的值。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM