コード例 #1
0
 public DatabaseManage(SettingsModel settings)
 {
     Host = settings.Host;
     Port = settings.Port;
     User = settings.User;
     Password = settings.Password;
     Database = settings.Database;
 }
コード例 #2
0
        public static void SetXml(SettingsModel x)
        {
            // Comprobamos si existe el directorio
            CheckIfDirectoryExists();

            List<SettingsModel> models = new List<SettingsModel>();

            // Si existe el fichero, lo borramos
            if (CheckIfFileExists())
            {
                // Cargamos el fichero XML
                XDocument doc = XDocument.Load(_fullFilePath);

                // Obtenemos los datos cargados
                models = doc.Descendants("Settings").Select(s => new SettingsModel
                {
                    Host = (string)s.Attribute("HOST"),
                    Port = (int)s.Attribute("PORT"),
                    User = (string)s.Attribute("USER"),
                    Password = (string)s.Attribute("PASS"),
                    Database = (string)s.Attribute("DB"),
                    Destiny = (string)s.Attribute("DESTINY")
                }).ToList();

                // Dependiendo de que vayamos a cargar, lo eliminamos
                var toRemove = models.FirstOrDefault(s => s.Destiny == x.Destiny);
                if (toRemove != null)
                    models.Remove(toRemove);

                // Borramos el fichero
                File.Delete(_fullFilePath);
            }

            // Añadimos el actual
            models.Add(x);

            // Creamos un nuevo XDocument
            XDocument xDocument = new XDocument();

            // Creamos un elemento
            XElement sectionXML = new XElement("SettingsData", (
                models.Select(y => new XElement("Settings",
                    new XAttribute("HOST", y.Host ?? "127.0.0.1"),
                    new XAttribute("PORT", y.Port),
                    new XAttribute("USER", y.User ?? "postgres"),
                    new XAttribute("PASS", y.Password ?? ""),
                    new XAttribute("DB", y.Database ?? "postgres"),
                    new XAttribute("DESTINY", y.Destiny ?? "SOURCE")
            ))));

            // Lo añadimos al XDocument
            xDocument.Add(sectionXML);

            // Guardamos el XDocument en disco
            xDocument.Save(_fullFilePath, SaveOptions.None);
        }