简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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