示例#1
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                products = new ProductsXmlCollection(txtSourceXml.Text);

                grid.DataSource = products.DataTable;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public List<ProductModel> GetSourceProductsXml(string xmlPath, string priceColumn)
        {
            //var xmlPath = @"C:\Users\Ertan\Downloads\N11-XML.xml";
            //var xmlPath = @"http://www.elektrostil.com/index.php?do=catalog/output&pCode=9211982202";

            if (this.ConvertedDataSet != null)
            {
                var xmlColl = new ProductsXmlCollection(xmlPath, this.ConvertedDataSet);
                xmlColl.ApplyTransforms();
                var productList = new List<ProductModel>();

                foreach (DataRow dr in xmlColl.DataTable.Rows)
                {
                    var prod = new ProductModel
                    {
                        //id = dr["id"],
                        title = (string)dr["label"],
                        stockCode = (string)dr["stockCode"],
                        //displayPrice = calculatePrice((string)dr[priceColumn], (string)dr["currencyAbbr"], (string)dr["tax"]),
                        displayPrice = decimal.Parse(dr[priceColumn].ToString().Replace(".", ",")),
                        stockAmount = int.Parse(dr["stockAmount"].ToString()),

                        label = (string)dr["label"],
                        brand = (string)dr["brand"],
                        mainCategory = (string)dr["mainCategory"],
                        category = (string)dr["category"],
                        subCategory = (string)dr["subCategory"],

                        picture1Path = (string)dr["picture1Path"],
                        //picture2Path = (string)dr["picture2Path"],
                        //picture3Path = (string)dr["picture3Path"],
                        //picture4Path = (string)dr["picture4Path"],

                        details = (string)dr["details"],
                        //rebatedPriceWithoutTax = (string)dr["rebatedPriceWithoutTax"],
                    };
                    productList.Add(prod);

                    // Stok değeri 1 olan ürünleri kaldır.
                    var subList = productList.Where(x => x.stockAmount == 1).ToList();
                    productList.RemoveAll(x => subList.Any(y => y == x));

                }

                return productList;
            }
            else
            {

                var dt = new DataTable("ProductsXml");

                var xdoc = XDocument.Load(xmlPath);

                var q = from d in xdoc.Root.Descendants("item")
                    select d;
                var list = q.ToList();

                foreach (var xElement in list)
                {
                    //var valueList = new List<object>();
                    foreach (var attr in xElement.Elements())
                    {
                        if (dt.Columns[attr.Name.LocalName] == null)
                            dt.Columns.Add(attr.Name.LocalName);
                    }
                    var dr = dt.NewRow();
                    foreach (var attr in xElement.Elements())
                    {
                        dr[attr.Name.LocalName] = attr.Value;
                    }

                    dt.Rows.Add(dr);
                }

                var kurTable = GetDovizKurlari();
                var usdKur = kurTable["USD"];
                var eurKur = kurTable["EUR"];
                var karAmount = 0;

                if (usdKur < 1 || eurKur < 1)
                {
                    throw new Exception("Kurlarda bir hata var.");
                }

                var productList = new List<ProductModel>();
                Func<string, string, string, decimal> calculatePrice = (x, curr, kdv) =>
                {
                    var price = decimal.Parse(x.Replace(".", ","));
                    if (curr == "USD")
                        price = price*usdKur;
                    else if (curr == "EUR")
                        price = price*eurKur;
                    else if (curr != "TL")
                        throw new NotImplementedException();

                    price = price*(int.Parse(kdv) + 100)/100;
                    price = Math.Round(price*100)/100;
                    price += karAmount;
                    return price;
                };

                foreach (DataRow dr in dt.Rows)
                {
                    var prod = new ProductModel
                    {
                        //id = dr["id"],
                        title = (string) dr["label"],
                        stockCode = (string) dr["stockCode"],
                        displayPrice = calculatePrice((string) dr[priceColumn], (string) dr["currencyAbbr"], (string) dr["tax"]),
                        stockAmount = int.Parse(dr["stockAmount"].ToString()),

                        label = (string) dr["label"],
                        brand = (string) dr["brand"],
                        mainCategory = (string) dr["mainCategory"],
                        category = (string) dr["category"],
                        subCategory = (string) dr["subCategory"],

                        picture1Path = (string) dr["picture1Path"],
                        //picture2Path = (string)dr["picture2Path"],
                        //picture3Path = (string)dr["picture3Path"],
                        //picture4Path = (string)dr["picture4Path"],

                        details = (string) dr["details"],
                        //rebatedPriceWithoutTax = (string)dr["rebatedPriceWithoutTax"],
                    };
                    productList.Add(prod);

                    // Stok değeri 1 olan ürünleri kaldır.
                    var subList = productList.Where(x => x.stockAmount == 1).ToList();
                    productList.RemoveAll(x => subList.Any(y => y == x));

                }
                return productList;
            }
        }