예제 #1
0
        public static string ParseXML(XDocument d)
        {
            LoadSource();
            countMistakes = 0;
            countSuccess  = 0;
            curCount      = 1;
            Mistakes      = "";

            foreach (XElement add in d.Root.Elements())
            {
                var from = add.Element("from");
                var to   = add.Element("to");

                surnameFrom       = from.Attribute("surname")?.Value;
                nameFrom          = from.Attribute("name")?.Value;
                midnameFrom       = from.Attribute("midname")?.Value;
                companyFrom       = from.Attribute("company")?.Value;
                phoneFrom         = from.Attribute("phone")?.Value;
                mailFrom          = from.Attribute("mail")?.Value;
                indexFrom         = from.Attribute("index")?.Value;
                addressFrom       = from.Attribute("address")?.Value;
                cityFrom          = from.Attribute("city")?.Value;
                informingSMSFrom  = Convert.ToBoolean(from.Attribute("informingSMS") == null ? "false" : from.Attribute("informingSMS").Value);
                informingMailFrom = Convert.ToBoolean(from.Attribute("informingMail") == null ? "false" : from.Attribute("informingMail").Value);

                surnameTo       = to.Attribute("surname")?.Value;
                nameTo          = to.Attribute("name")?.Value;
                midnameTo       = to.Attribute("midname")?.Value;
                companyTo       = to.Attribute("company")?.Value;
                phoneTo         = to.Attribute("phone")?.Value;
                mailTo          = to.Attribute("mail")?.Value;
                indexTo         = to.Attribute("index")?.Value;
                addressTo       = to.Attribute("address")?.Value;
                cityTo          = to.Attribute("city")?.Value;
                informingSMSTo  = Convert.ToBoolean(to.Attribute("informingSMS") == null ? "false" : to.Attribute("informingSMS").Value);
                informingMailTo = Convert.ToBoolean(to.Attribute("informingMail") == null ? "false" : to.Attribute("informingMail").Value);

                var temp = from.Attribute("idCentre");
                idCentreFrom = temp == null ? null : (int?)Convert.ToInt32(temp.Value);
                temp         = to.Attribute("idCentre");
                idCentreTo   = temp == null ? null : (int?)Convert.ToInt32(temp.Value);

                weightDouble = add.Attribute("weight") == null ? (double?)null : Convert.ToDouble(add.Attribute("weight").Value, CultureInfo.InvariantCulture);
                lengthDouble = add.Attribute("length") == null ? (double?)null : Convert.ToDouble(add.Attribute("length").Value, CultureInfo.InvariantCulture);
                widthtDouble = add.Attribute("width") == null ? (double?)null : Convert.ToDouble(add.Attribute("width").Value, CultureInfo.InvariantCulture);
                heightDouble = add.Attribute("height") == null ? (double?)null : Convert.ToDouble(add.Attribute("height").Value, CultureInfo.InvariantCulture);
                temp         = add.Attribute("number");
                numberInt    = temp == null ? null : (int?)Convert.ToInt32(temp.Value);
                temp         = add.Attribute("value");
                valueDouble  = temp == null ? null : (int?)Convert.ToDouble(temp.Value, CultureInfo.InvariantCulture);
                description  = add.Attribute("description")?.Value;
                tariff       = add.Attribute("tariff")?.Value;

                if (!CheckFromTo(ref surnameFrom, ref nameFrom, ref midnameFrom, ref companyFrom, ref phoneFrom, ref mailFrom, ref indexFrom,
                                 ref addressFrom, ref idCentreFrom, out idCityFrom, cityFrom, informingSMSFrom, informingMailFrom))
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }
                if (!CheckFromTo(ref surnameTo, ref nameTo, ref midnameTo, ref companyTo, ref phoneTo, ref mailTo, ref indexTo,
                                 ref addressTo, ref idCentreTo, out idCityTo, cityTo, informingSMSTo, informingMailTo))
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }
                if (!CheckPackage())
                {
                    countMistakes++;
                    Mistakes += curCount + ", ";
                    curCount++;
                    continue;
                }

                using (ModelDBContainer db = new ModelDBContainer())
                {
                    Person From = new Person
                    {
                        Name          = nameFrom,
                        MiddleName    = midnameFrom,
                        Surname       = surnameFrom,
                        Company       = companyFrom,
                        Phone         = phoneFrom,
                        Mail          = mailFrom,
                        Index         = indexFrom,
                        CityId        = idCityFrom,
                        Address       = addressFrom,
                        CentreId      = idCentreFrom,
                        InformingSMS  = informingSMSFrom,
                        InformingMail = informingMailFrom
                    };
                    Person To = new Person
                    {
                        Name          = nameTo,
                        MiddleName    = midnameTo,
                        Surname       = surnameTo,
                        Company       = companyTo,
                        Phone         = phoneTo,
                        Mail          = mailTo,
                        Index         = indexTo,
                        CityId        = idCityTo,
                        Address       = addressTo,
                        CentreId      = idCentreTo,
                        InformingSMS  = informingSMSTo,
                        InformingMail = informingMailTo
                    };
                    db.Persons.Add(From); db.Persons.Add(To);
                    db.SaveChanges();
                    Package package = new Package
                    {
                        PersonIdFrom     = From.Id,
                        PersonIdTo       = To.Id,
                        Weight           = weightDouble,
                        Width            = widthtDouble,
                        Length           = lengthDouble,
                        Height           = heightDouble,
                        DeclaredValue    = valueDouble,
                        Cost             = costDouble,
                        TariffId         = idTariff,
                        Description      = description,
                        NumberOfPackages = numberInt == null ? 1 : (int)numberInt
                    };
                    db.Packages.Add(package);
                    db.SaveChanges();
                    db.Records.Add(new Record {
                        PackageId = package.Id, DateAndTime = DateTime.Now, Status = RegisterStatus
                    });
                    db.SaveChanges();
                }
                countSuccess++;
                curCount++;
            }
            string message = "Всего записей об отправлениях: " + (curCount - 1) +
                             ". Успешно загружено: " + countSuccess + ". Не  загружено по причине ошибки: " + countMistakes + ".";

            if (Mistakes.Length > 0)
            {
                message += " Порядковые номера записей с ошибками: " + Mistakes.Substring(0, Mistakes.Length - 2) + ".";
            }
            return(message);
        }