Advertisement
vlad0

Untitled

Jul 23rd, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using System.Globalization;
  8. using JsonReport;
  9. using Catalog.Data;
  10. using Catalog.Model;
  11.  
  12. namespace ReadXMLFile
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             string path = @"../../file.xml";
  19.             Dictionary<string, Dictionary<string, decimal>> data = new Dictionary<string, Dictionary<string, decimal>>();
  20.  
  21.  
  22.             XmlDocument doc = new XmlDocument();
  23.             doc.Load(@"../../file.xml");
  24.             XmlNode rootNode = doc.DocumentElement;
  25.             //Console.WriteLine("Root node: {0}", rootNode.Name);
  26.  
  27.             decimal expense = 0m;
  28.             string txt = "";
  29.             foreach (XmlNode node in rootNode.ChildNodes)
  30.             {
  31.                 string vendor = node.Attributes["vendor"].Value.ToString();
  32.                 string month = "";
  33.                 //Console.WriteLine(vendor);
  34.                 foreach (XmlNode child in node)
  35.                 {
  36.                     month = child.Attributes["month"].Value.ToString();
  37.                     txt = child.InnerText.ToString();
  38.                     expense = decimal.Parse(txt, CultureInfo.InvariantCulture);
  39.  
  40.                     if (!data.ContainsKey(vendor))
  41.                     {
  42.                         data[vendor] = new Dictionary<string, decimal>();
  43.                     }
  44.                     data[vendor].Add(month, expense);
  45.                     VendorReport a = new VendorReport(vendor, month, expense);
  46.                     JsonClient.InsertIntoMongoVendorReport(a);
  47.                 }
  48.             }
  49.  
  50.             using (var db = new CatalogContext())
  51.             {
  52.                 //one query to database
  53.                 var currentVendors = db.Vendors.ToList();
  54.                 var currentExpenses = db.VendorExpenses.ToList();
  55.  
  56.                 foreach (var vendor in data)
  57.                 {
  58.  
  59.                     int vendorID = currentVendors
  60.                         .Where(x => x.Name.CompareTo(vendor.Key) == 0)
  61.                         .Select(x => x.VendorId).First();
  62.  
  63.                     foreach (var vendorExpense in vendor.Value)
  64.                     {
  65.                         VendorExpense newExpense = new VendorExpense
  66.                             {
  67.                                 VendorID = vendorID,
  68.                                 Month = DateTime.Parse(vendorExpense.Key),
  69.                                 Expense = vendorExpense.Value
  70.                             };
  71.                         var currentExpense = currentExpenses.Where(x => x.VendorID == vendorID && x.Month == DateTime.Parse(vendorExpense.Key)).FirstOrDefault();
  72.                         if (currentExpense != null)
  73.                         {
  74.                             currentExpense.Expense = vendorExpense.Value;
  75.                         }
  76.                         else
  77.                         {
  78.                             db.VendorExpenses.Add(newExpense);
  79.                         }
  80.                     }
  81.                 }
  82.                 db.SaveChanges();
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement