Пример #1
0
        public static bool Devolver()
        {
            bool sucesso = false;

            try
            {
                OpenFileDialog o = new OpenFileDialog();
                o.ShowDialog();
                o.DefaultExt = "*.txt";

                String receber = File.ReadAllText(o.FileName);

                char d = '\n';
                char f = '|';



                String[] dadosPorEnter     = receber.Split(d);
                int      dadosPorEnterSize = dadosPorEnter.Length;

                String[] dado;


                List <String>  dados = new List <String>();
                XmlDocument    doc   = new XmlDocument();
                XmlDeclaration dec   = doc.CreateXmlDeclaration("1.0", null, null);
                doc.AppendChild(dec);
                XmlElement
                    root = doc.CreateElement(
                    "DonatorsList");     // Criar um root onde os Elementos Donators irão ser introduzidos
                doc.AppendChild(root);

                for (int i = 1; i < dadosPorEnterSize; i++)
                {
                    dados.Add(dadosPorEnter[i]); //adicionar pessoa
                }


                for (int j = 0;
                     j < dados.Count - 1;
                     j++)                                 //dados -1 para não contar o ultimo elemento da lista dados que esta em branco
                {
                    dado = dados[j].Split(f);             //dividir pessoa por pipe

                    for (int k = 0; k < dado.Length; k++) //remove os espaços e o caracter especial no 1
                    {
                        //dado[k] = Regex.Replace(dado[k], @"\s+", " "); //remove espaços

                        dado[k]  = dado[k].Trim();
                        dado[0]  = Regex.Replace(dado[0], @"§", ""); //remove caracter especial que apenas aparece no primeiro elemento
                        dado[19] = dado[19].Replace('.', ',');
                    }


                    //chamada do Metodo AddDonator a cada iteração que ira introduzir um Elemento Donator na DonatorsList
                    root.AppendChild(AddNewDonator.AddDonator(dado[0], dado[1], dado[2], dado[3],
                                                              dado[4], dado[5], dado[6], dado[7], dado[8],
                                                              dado[9], dado[10], dado[11], dado[12], dado[13],
                                                              dado[14], dado[15], dado[16], dado[17], dado[18],
                                                              dado[19], dado[20], dado[21], dado[22],
                                                              dado[23], doc));

                    doc.Save(@"BaseDados.xml");
                    sucesso = true;
                }
            }
            catch (Exception e)
            {
                sucesso = false;
            }

            return(sucesso);
        }
        private static bool exportJSON(ServiceReference1.Donator[] donators, List <int> id, string path)
        {
            String number;
            String sexo;
            String firstName;
            String lastName;
            String streetAddress;
            String city;
            String statefull;
            String zipCode;
            String eMail;
            String userName;
            String password;
            String telephoneNumber;
            String mothersMaiden;
            String birthDay;
            String age;
            String occupation;
            String company;
            String vehicle;
            String bloodType;
            String kilograms;
            String centimeters;
            String guid;
            String latitude;
            String longitude;

            XmlDocument docExportar = new XmlDocument();


            XmlElement rootExportar = docExportar.CreateElement("DonatorsList"); // Criar um root onde os Elementos Donators irão ser introduzidos

            docExportar.AppendChild(rootExportar);



            for (int i = 0; i < id.Count(); i++)
            {
                // Defesa do projeto!
                //  foreach (BloodDonator b in donators.Where(n => n.Number == id[i]).Where(a => a.BloodType.Equals("A+")))
                foreach (var b in donators.Where(n => n.Number == id[i]))
                {
                    number          = Convert.ToString(b.Number);
                    sexo            = b.Sexo;
                    firstName       = b.FirstName;
                    lastName        = b.LastName;
                    streetAddress   = b.StreetAddress;
                    city            = b.City;
                    statefull       = b.Statefull;
                    zipCode         = b.ZipCode;
                    eMail           = b.EMail;
                    userName        = b.UserName;
                    password        = b.Password;
                    telephoneNumber = Convert.ToString(b.TelephoneNumber);
                    mothersMaiden   = b.MothersMaiden;
                    birthDay        = Convert.ToString(b.BirthDay);
                    age             = Convert.ToString(b.Age);
                    occupation      = b.Occupation;
                    company         = b.Company;
                    vehicle         = b.Vehicle;
                    bloodType       = b.BloodType;
                    kilograms       = Convert.ToString(b.Kilograms);
                    centimeters     = Convert.ToString(b.Centimeters);
                    guid            = b.Guid;
                    latitude        = b.Latitude;
                    longitude       = b.Longitude;

                    rootExportar.AppendChild(AddNewDonator.AddDonator
                                                 (number, sexo, firstName, lastName, streetAddress, city, statefull, zipCode, eMail, userName,
                                                 password,
                                                 telephoneNumber, mothersMaiden, birthDay, age, occupation, company, vehicle, bloodType,
                                                 kilograms, centimeters,
                                                 guid, latitude, longitude, docExportar

                                                 ));
                }
            }
            String jsonText = "";

            jsonText += JsonConvert.SerializeObject(rootExportar);
            File.WriteAllText(path, jsonText);

            return(true);
        }
        //metodo responsável por exportar em xml
        private static bool exportXML(List <BloodDonator> donators, List <int> id, string path)
        {
            String number;
            String sexo;
            String firstName;
            String lastName;
            String streetAddress;
            String city;
            String statefull;
            String zipCode;
            String eMail;
            String userName;
            String password;
            String telephoneNumber;
            String mothersMaiden;
            String birthDay;
            String age;
            String occupation;
            String company;
            String vehicle;
            String bloodType;
            String kilograms;
            String centimeters;
            String guid;
            String latitude;
            String longitude;

            XmlDocument    docExportar = new XmlDocument();
            XmlDeclaration decExportar = docExportar.CreateXmlDeclaration("1.0", null, null);

            docExportar.AppendChild(decExportar);
            XmlElement rootExportar = docExportar.CreateElement("DonatorsList"); // Criar um root onde os Elementos Donators irão ser introduzidos

            docExportar.AppendChild(rootExportar);

            for (int i = 0; i < id.Count(); i++)
            {
                foreach (BloodDonator b in donators.Where(n => n.Number == id[i]))
                {
                    number          = Convert.ToString(b.Number);
                    sexo            = b.Sexo;
                    firstName       = b.FirstName;
                    lastName        = b.LastName;
                    streetAddress   = b.StreetAddress;
                    city            = b.City;
                    statefull       = b.Statefull;
                    zipCode         = b.ZipCode;
                    eMail           = b.Email;
                    userName        = b.UserName;
                    password        = b.Password;
                    telephoneNumber = Convert.ToString(b.TelephoneNumber);
                    mothersMaiden   = b.MothersMaiden;
                    birthDay        = Convert.ToString(b.BirthDay);
                    age             = Convert.ToString(b.Age);
                    occupation      = b.Occupation;
                    company         = b.Company;
                    vehicle         = b.Vehicle;
                    bloodType       = b.BloodType;
                    kilograms       = Convert.ToString(b.Kilograms);
                    centimeters     = Convert.ToString(b.Centimeters);
                    guid            = b.Guid;
                    latitude        = b.Latitude;
                    longitude       = b.Longitude;

                    rootExportar.AppendChild(AddNewDonator.AddDonator
                                                 (number, sexo, firstName, lastName, streetAddress, city, statefull, zipCode, eMail, userName, password,
                                                 telephoneNumber, mothersMaiden, birthDay, age, occupation, company, vehicle, bloodType, kilograms, centimeters,
                                                 guid, latitude, longitude, docExportar

                                                 ));

                    docExportar.Save(path);
                }
            }
            return(true);
        }