public IDictionary<string, ApplicationConfig> ReadXml(string path)
        {
            var applications = XDocument.Load(path).Descendants("application");
            var dictionary = new Dictionary<string, ApplicationConfig>();
            foreach (var application in applications) {
                var dbCredentials = new List<DbConfig>();
                var ftpCredentials = new List<FtpCredential>();
                var files = new List<string>();
                var datas = application.Descendants("db");
                var ftps = application.Descendants("ftp");
                var fs = application.Elements("fs");
                var defautlFtpCredentials= new List<FtpCredential>();
                var defaultDbCredentials = ReadDefaultXmlForDb(path);
                if (datas.Any()) {
                    foreach (var data in datas) {
                        var password = data.Element("db_password") == null ? defaultDbCredentials.ElementAt(0).Password : data.Element("db_password").Value;
                        var server = data.Element("db_server") == null ? defaultDbCredentials.ElementAt(0).Server : data.Element("db_server").Value;
                        var user = data.Element("db_user") == null ? defaultDbCredentials.ElementAt(0).UserName : data.Element("db_user").Value;
                        var dbName = data.Element("db_name").Value;
                        var db = new DbConfig {DbName = dbName, Server = server, Password = password, UserName = user};
                        dbCredentials.Add(db);
                    }
                }
                defautlFtpCredentials = ReadDefaultXmlForFtp(path);
                if (!ftps.Any()) {
                    ftpCredentials = ReadDefaultXmlForFtp(path);

                } else {
                    foreach (var ftp in ftps) {
                        var password = ftp.Element("ftp_password")==null?defautlFtpCredentials.ElementAt(0).PassWord:ftp.Element("ftp_password").Value;
                        var username = ftp.Element("ftp_user") == null ? defautlFtpCredentials.ElementAt(0).UserName : ftp.Element("ftp_user").Value; ;
                        var host = ftp.Element("ftp_host") == null ? defautlFtpCredentials.ElementAt(0).Host : ftp.Element("ftp_host").Value; ;
                        var port = ftp.Element("ftp_directory")== null ? defautlFtpCredentials.ElementAt(0).Directory : ftp.Element("ftp_directory").Value; ;
                        var ftpCredential = new FtpCredential
                                            {UserName = username, PassWord = password, Host = host, Directory = port};
                        ftpCredentials.Add(ftpCredential);
                    }
                }
                foreach (var xElement in fs) {
                    var value = xElement.Value;
                    files.Add(value);
                }

                dictionary.Add(application.Attribute("name").Value,
                                   new ApplicationConfig {DbConfigs = dbCredentials, FtpCredentials = ftpCredentials,FileDirectories = files});

            }
               return dictionary;
        }
        public List<FtpCredential> ReadDefaultXmlForFtp(string name)
        {
            var applications = XDocument.Load(name).Descendants("default");
            var ftpCredentials = new List<FtpCredential>();
            foreach (var application in applications) {

                var datas = application.Descendants("ftp");

                foreach (var data in datas) {
                    var password = data.Element("ftp_password").Value;
                    var host = data.Element("ftp_host").Value;
                    var user = data.Element("ftp_user").Value;
                    var port = data.Element("ftp_directory").Value;
                    var db = new FtpCredential {  PassWord = password, UserName = user ,Host = host,Directory = port};
                    ftpCredentials.Add(db);
                }
            }
            return ftpCredentials;
        }
        public FtpCredential ReadXmlForDefaultFtpCredentials(string path)
        {
            var defaultXmlConfig = XDocument.Load(path).Descendants("default").FirstOrDefault();

            if (defaultXmlConfig == null)
                return null;

            var defaultFtpConfigXml = defaultXmlConfig.Descendants("ftp").FirstOrDefault();

            if (defaultFtpConfigXml == null)
                return null;

            var password = defaultFtpConfigXml.Element("ftp_password").Value;
            var host = defaultFtpConfigXml.Element("ftp_host").Value;
            var user = defaultFtpConfigXml.Element("ftp_user").Value;
            var port = defaultFtpConfigXml.Element("ftp_directory").Value;
            var ftpCredentials = new FtpCredential { Password = password, Username = user, Host = host, Directory = port };

            return ftpCredentials;
        }
        public IEnumerable<Settings> ReadXml(string path)
        {
            var applications = XDocument.Load(path).Descendants("application");
            //var dictionary = new Dictionary<string, ApplicationConfig>();
            foreach (var application in applications) {

                var ftpCredentials = new List<FtpCredential>();

                var databases = application.Descendants("db");
                var ftps = application.Descendants("ftp");
                var fs = application.Elements("fs");
                var defaultFtpCredentials = ReadXmlForDefaultFtpCredentials(path);
                var defaultDbCredentials = ReadXmlForDefaultDbCredentials(path);
                var retentionDayCount = 0;
                var applicationName = application.Attribute("name") == null
                                          ? string.Empty
                                          : application.Attribute("name").Value;

                if (application.Element("retention_days") != null) {
                    int.TryParse(application.Element("retention_days").Value, out retentionDayCount);
                }

                var dbCredentials = from data in databases
                                    let password =
                                        data.Element("db_password") == null
                                            ? defaultDbCredentials.Password
                                            : data.Element("db_password").Value
                                    let server =
                                        data.Element("db_server") == null
                                            ? defaultDbCredentials.Server
                                            : data.Element("db_server").Value
                                    let user =
                                        data.Element("db_user") == null
                                            ? defaultDbCredentials.Username
                                            : data.Element("db_user").Value
                                    let dbName = data.Element("db_name").Value
                                    select
                                        new DatabaseConfig { DatabaseName = dbName, Server = server, Password = password, Username = user };

                foreach (var ftp in ftps) {
                    var password = ftp.Element("ftp_password") == null ? defaultFtpCredentials.Password : ftp.Element("ftp_password").Value;
                    var username = ftp.Element("ftp_user") == null ? defaultFtpCredentials.Username : ftp.Element("ftp_user").Value; ;
                    var host = ftp.Element("ftp_host") == null ? defaultFtpCredentials.Host : ftp.Element("ftp_host").Value; ;
                    var directory = ftp.Element("ftp_directory") == null ? defaultFtpCredentials.Directory : ftp.Element("ftp_directory").Value; ;
                    var ftpCredential = new FtpCredential { Username = username, Password = password, Host = host, Directory = directory };
                    ftpCredentials.Add(ftpCredential);
                }

                //if no credential is present, then add default credential
                if (!ftpCredentials.Any())
                    ftpCredentials.Add(defaultFtpCredentials);

                yield return Settings.Create(
                    applicationName,
                    dbCredentials,
                    fs.Select(xElement => xElement.Value).ToList(),
                    new NetworkCredential(ftpCredentials.First().Username, ftpCredentials.First().Password),
                    ftpCredentials.First().Directory,
                    ftpCredentials.First().Host,
                    retentionDayCount);

            }
        }