Thursday, May 16, 2019

CMD - Simple Find duplicated keys in XML file


 class XmlDuplicateCleaner
    {
        static void Main(string[] args)
        {
            Console.WriteLine("In what node should we search for duplicates?");
            var nodeToLookFor  = Console.ReadLine();
            Console.WriteLine("Provide filename for the file we should search");
            var filnamn = Console.ReadLine();
             var xmlDoc =  XDocument.Load(String.IsNullOrEmpty(filnamn) ? "xmlfile.xml" : filnamn);

             if (xmlDoc.Root != null)
             {
                 var grouped = xmlDoc.Root.Elements().Descendants(nodeToLookFor);
                 List<string> resultList = grouped.Select(groupItem => groupItem.Value).ToList();

                 var duplicateKeys = resultList.GroupBy(x => x)
                     .Where(group => @group.Count() > 1)
                     .Select(group => @group.Key);
                 foreach (var duplicateKey in duplicateKeys)
                 {
                     Console.WriteLine(duplicateKey);
                 }
             }

             Console.ReadLine();
        }
    }

No comments:

Post a Comment