Exemple #1
0
        public static void SaveProduct(Product pr)
        {
            if (pr == null)
                return;

            Char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
                DialogResult res = MessageBox.Show("Сохранить изменения?", "Программа ",
                MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (res == DialogResult.Yes)
            {
                XmlElement xRoot = pr.Xml.DocumentElement;

                foreach (XmlElement xnode in xRoot)
                {
                    if (xnode.GetHashCode() == pr.XmlIndex)
                    {
                        XmlNode type = xnode.Attributes.GetNamedItem("type");

                        type.Value = pr.Type;

                        XmlNode name = xnode.Attributes.GetNamedItem("name");

                        name.Value = pr.Name;

                        XmlNode fat = xnode.Attributes.GetNamedItem("fat");

                        fat.Value = pr.Fat;

                        foreach (XmlNode childnode in xnode.ChildNodes)
                        {
                            if (childnode.Name == "NormalizedMixture")
                            {
                                double.Parse(childnode.InnerText = pr.NormalizedMixture.ToString());

                                XmlNode mixName = childnode.Attributes.GetNamedItem("name");
                                if (mixName != null)
                                    mixName.Value = pr.MixtureName;

                                XmlNode mixFat = childnode.Attributes.GetNamedItem("fat");
                                if (mixFat != null)
                                    mixFat.Value = pr.MixtureFat;
                            }
                            if (childnode.Name == "milk_base")
                            {
                                double.Parse(childnode.InnerText = pr.MilkBaseValue.ToString());

                                XmlNode milkFat = childnode.Attributes.GetNamedItem("fat");
                                if (milkFat != null)
                                    milkFat.Value = pr.MilkBaseFat;
                            }
                            if (childnode.Name == "milk_nofat")
                            {
                                double.Parse(childnode.InnerText = pr.MilkNofatValue.ToString());
                            }
                            if (childnode.Name == "Plotn")
                            {
                                Double.Parse(childnode.InnerText = pr.Plotn.ToString());
                            }
                            if (childnode.Name == "image_name")
                            {
                                pr.ImageName = childnode.InnerText;
                            }
                            else { pr.ImageName = ("notfound"); }
                        }

                    }

                }
                try
                {
                    pr.Xml.Save(@"../../Xmls/products.xml");
                    //MessageBox.Show("Данные успешно сохранены.", "Сообщение.");
                }
                catch
                {
                    MessageBox.Show("Невозможно сохранить XML файл.", "Ошибка.");
                }
            }
            if (res == DialogResult.No)
            {
                
                return;
            }
        }
Exemple #2
0
        public static List<Product> GetProduct() //считывание файла products xml
        {
            Char separator = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
            System.Globalization.NumberStyles style;
            System.Globalization.CultureInfo culture;
            style = System.Globalization.NumberStyles.Number |
            System.Globalization.NumberStyles.AllowCurrencySymbol;
            culture = System.Globalization.CultureInfo.CurrentUICulture;

            string pathFile = @"../../Xmls/products.xml";
            if (!File.Exists(pathFile)) //если файл не найден
            {
                MessageBox.Show("File products not found");
                return null;
            }
            else
            {
                try
                {
                    List<Product> products = new List<Product>();
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.Load(pathFile);
                    XmlElement xRoot = xDoc.DocumentElement;
                    foreach (XmlElement xnode in xRoot)
                    {
                        Product product = new Product();

                        product.Xml = xDoc;
                        product.XmlIndex = xnode.GetHashCode();

                        foreach (XmlNode childnode in xnode.ChildNodes)
                        {
                            XmlNode type = xnode.Attributes.GetNamedItem("type");
                            if (type == null)
                                return null;

                            product.Type = type.Value;

                            XmlNode name = xnode.Attributes.GetNamedItem("name");
                            if (name == null)
                                return null;

                            product.Name = name.Value;

                            XmlNode fat = xnode.Attributes.GetNamedItem("fat");
                            if (fat == null)
                                return null;

                            product.Fat = fat.Value;

                            if (childnode.Name == "NormalizedMixture")
                            {
                                if (Double.TryParse(childnode.InnerText.Replace('.', separator), style, culture, out double result))
                                {
                                    product.NormalizedMixture = result;
                                }
                                else { MessageBox.Show("Значение тега NormalizedMixture неверно ", "Ошибка"); product.NormalizedMixture = 0; }

                                XmlNode mixName = childnode.Attributes.GetNamedItem("name");
                                if (mixName != null)
                                    product.MixtureName = mixName.Value;

                                XmlNode mixFat = childnode.Attributes.GetNamedItem("fat");
                                if (mixFat != null)
                                    product.MixtureFat = mixFat.Value;
                            }
                            if (childnode.Name == "milk_base")
                            {
                                if (Double.TryParse(childnode.InnerText.Replace('.', separator), style, culture, out double result))
                                {
                                    product.MilkBaseValue = result;
                                }
                                else { MessageBox.Show("Значение тега milk_base неверно ", "Ошибка"); product.MilkBaseValue = 0; }

                                XmlNode milkFat = childnode.Attributes.GetNamedItem("fat");
                                if (milkFat != null)
                                    product.MilkBaseFat = milkFat.Value;
                            }
                            if (childnode.Name == "milk_nofat")
                            {
                                if (Double.TryParse(childnode.InnerText.Replace('.', separator), style, culture, out double result))
                                {
                                    product.MilkNofatValue = result;
                                }
                                else { MessageBox.Show("Значение тега milk_nofat неверно ", "Ошибка"); product.MilkNofatValue = 0; }
                            }
                            if (childnode.Name == "Performance")
                            {
                                if (Double.TryParse(childnode.InnerText.Replace('.', separator), style, culture, out double result))
                                {
                                    product.Performance = result;
                                }
                                else { MessageBox.Show("Значение тега Performance неверно ", "Ошибка"); product.Performance = 0; }
                            }
                            if (childnode.Name == "Plotn")
                            {
                                if (Double.TryParse(childnode.InnerText.Replace('.', separator), style, culture, out double result))
                                {
                                    product.Plotn = result;
                                }
                                else { MessageBox.Show("Значение тега Plotn неверно ", "Ошибка"); product.Plotn = 0; }
                            }
                            if (childnode.Name == "image_name")
                            {
                                product.ImageName = childnode.InnerText;
                            }
                            else { product.ImageName = ("notfound"); }
                        }
                        products.Add(product);
                    }

                    return products;
                }

                catch(Exception exc)
                {
                    MessageBox.Show("Ошибка чтения файла. \n " + exc.ToString(), "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
            }

        }