Пример #1
0
        public void RemoveDuplicationsFromRegularServersFiles(ServersFile commonServersFile, List <ServersFile> regularServersFiles)
        {
            if (commonServersFile == null)
            {
                return;
            }
            foreach (var regularServersFile in regularServersFiles)
            {
                foreach (var commonServer in commonServersFile.Servers)
                {
                    var fileNameArray     = regularServersFile.PathToFile.Split('\\');
                    var fileName          = fileNameArray[fileNameArray.Length - 2];
                    var regularFileServer = regularServersFile.Servers.First(x => x.ServerType == commonServer.ServerType);

                    Console.WriteLine(fileName);
                    Console.WriteLine($"{fileName}-{commonServer.ServerType} items count {regularFileServer.Items.Count}");

                    var duplicatedItems = commonServer.Items.Intersect(regularFileServer.Items).ToList();
                    if (duplicatedItems.Count > 0)
                    {
                        Console.WriteLine($"{fileName}-{commonServer.ServerType} duplicated items count {duplicatedItems.Count}");
                    }

                    regularFileServer.Items = regularFileServer.Items.Except(duplicatedItems).ToList();
                    Console.WriteLine($"{fileName}-{commonServer.ServerType} unique items count {regularFileServer.Items.Count}");
                    Console.WriteLine();
                }
            }
        }
Пример #2
0
        public void MoveCommonItemsToCommonServersFiles(ServersFile commonServersFile, List <ServersFile> regularServersFiles)
        {
            if (commonServersFile == null || regularServersFiles.Count < 2)
            {
                return;
            }
            foreach (var serverType in (ServerType[])Enum.GetValues(typeof(ServerType)))
            {
                var listOfLists = new List <List <ServerItem> >();
                foreach (var regularServersFile in regularServersFiles)
                {
                    listOfLists.Add(regularServersFile.Servers.First(x => x.ServerType == serverType).Items);
                }
                var intersection = listOfLists
                                   .Skip(1)
                                   .Aggregate((previousList, nextList) => previousList.Intersect(nextList).ToList());

                if (intersection.Count <= 0)
                {
                    continue;
                }

                foreach (var regularServersFile in regularServersFiles)
                {
                    var regularServer = regularServersFile.Servers.First(x => x.ServerType == serverType);
                    regularServer.Items = regularServer.Items.Except(intersection).ToList();
                }

                var commonServer = commonServersFile.Servers.First(x => x.ServerType == serverType);
                commonServer.Items = commonServer.Items.Concat(intersection).ToList();
            }
        }
        public void Save(ServersFile file)
        {
            var serializer = new XmlSerializer(typeof(ServersFile));
            var ns         = new XmlSerializerNamespaces();

            ns.Add("", "");
            var xmlWriterSettings = new XmlWriterSettings()
            {
                OmitXmlDeclaration  = true,
                Indent              = true,
                NewLineOnAttributes = true,
                NewLineHandling     = NewLineHandling.None,
                CheckCharacters     = false,
                Encoding            = Encoding.GetEncoding("windows-1251")
            };

            using (var xmlWriter = XmlWriter.Create(file.PathToFile, xmlWriterSettings))
            {
                xmlWriter.WriteRaw("<?xml version=\"1.0\" encoding=\"windows-1251\" standalone=\"yes\"?>\r\n");
                serializer.Serialize(xmlWriter, file, ns);
            }
        }