Exemplo n.º 1
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);
        }
Exemplo n.º 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
                {
                    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);
        }