예제 #1
0
        private Dictionary <string, XDocument> FindFileNamePatternInDirectory(string directory)
        {
            var filePatternsToLookFor = FileNamePatterns.ToList();

            if (!string.IsNullOrEmpty(FileNamePattern))
            {
                filePatternsToLookFor.Add(FileNamePattern);
            }

            var filesFound = filePatternsToLookFor.SelectMany(filePattern =>
                                                              Directory.EnumerateFiles(directory, "*", SearchOption)
                                                              .Where(name => Regex.IsMatch(name, filePattern, IgnoreCase)));

            var filesAsXDocuments = new Dictionary <string, XDocument>();

            foreach (var fileFound in filesFound)
            {
                try
                {
                    filesAsXDocuments[fileFound] = XDocument.Load(fileFound);
                }
                catch (XmlException ex)
                {
                    Logger.LogError(ex, $"Could not parse xml file: {fileFound}.");
                }
            }

            return(filesAsXDocuments);
        }
예제 #2
0
        private Dictionary <string, XDocument> FindFileNamePatternInDirectory(string directory)
        {
            var filePatternsToLookFor = FileNamePatterns.Select(f =>
                                                                //Trim the expression being sent. We need this to support not changing the files until customers have moved to a later version:
                                                                f.Replace(".*(", "*").Replace(")$", string.Empty)
                                                                ).ToList();

            if (!string.IsNullOrEmpty(FileNamePattern))
            {
                filePatternsToLookFor.Add(FileNamePattern);
            }

            var filesFound = filePatternsToLookFor.SelectMany(filePattern => Directory.EnumerateFiles(directory, filePattern, SearchOption));

            var filesAsXDocuments = new Dictionary <string, XDocument>();

            foreach (var fileFound in filesFound)
            {
                try
                {
                    filesAsXDocuments[fileFound] = XDocument.Load(fileFound);
                }
                catch (XmlException ex)
                {
                    Logger.LogError(ex, $"Could not parse xml file: {fileFound}.");
                }
            }

            return(filesAsXDocuments);
        }
예제 #3
0
        private Dictionary <string, XDocument> FindFileNamePatternInDirectory(string directory)
        {
            var filePatternsToLookFor = FileNamePatterns.Select(f =>
                                                                //Trim the expression being sent. We need this to support not changing the files until customers have moved to a later version:
                                                                f.Replace(".*(", "*").Replace(")$", string.Empty)
                                                                ).ToList();

            if (!string.IsNullOrEmpty(FileNamePattern))
            {
                filePatternsToLookFor.Add(FileNamePattern);
            }

            var filesFound = filePatternsToLookFor.SelectMany(filePattern => Directory.EnumerateFiles(directory, filePattern, SearchOption));

            var filesAsXDocuments = new Dictionary <string, XDocument>();

            foreach (var fileFound in filesFound)
            {
                try
                {
                    if (File.Exists(fileFound))
                    {
                        string fileContent = File.ReadAllText(fileFound);
                        // fileFound could be an empty file we should check to see if the file is non-empty and starts with a < for a potentially valid XML file
                        if (!string.IsNullOrEmpty(fileContent) && fileContent.TrimStart().StartsWith("<"))
                        {
                            filesAsXDocuments[fileFound] = XDocument.Parse(fileContent);
                        }
                    }
                }
                catch (XmlException ex)
                {
                    Logger.LogError(ex, $"Could not parse xml file: {fileFound}.");
                }
            }

            return(filesAsXDocuments);
        }