Using LINQ to parse XML into Dictionary

前端 未结 3 1155
挽巷
挽巷 2021-01-06 03:35

I have a configuration file such as:


    
            


        
3条回答
  •  走了就别回头了
    2021-01-06 04:38

    It isn't necessary to have the query as you're just doing a projection. Move the projection into the call to ToDictionary():

    var configDictionary = xmlDocument.Descendants("Config")
                                      .ToDictionary(e => e.Attribute("name").Value,
                                                    e => e.Attribute("value").Value);
    

提交回复
热议问题