private void IngestNewReduxData(object sender, RoutedEventArgs e) { Services services = new Services(); var context = new ReduxEntities(); XElement doc = XElement.Load(@"D:\Docs\prog.txt"); var diskrefs = (from item in context.redux_items select item.disk_reference).ToArray(); var remapped = from row in doc.Elements("row") select new Dictionary <string, string>( (from field in row.Elements("field") select new { Key = field.Attribute("name").Value, Value = field.Value }).ToDictionary(vp => vp.Key, vp => vp.Value)); var joined = from row in doc.Elements("row") join dr in diskrefs on row.Elements("field").First(f => f.Attribute("name").Value == "disk_reference").Value equals dr into j from j2 in j.DefaultIfEmpty() where j2 == null select row; CultureInfo provider = CultureInfo.InvariantCulture; var items = from row in joined select new redux_items { aired = DateTime.Parse(row.Elements("field").First(f => f.Attribute("name").Value == "start").Value, provider, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal), disk_reference = row.Elements("field").First(f => f.Attribute("name").Value == "disk_reference").Value, programme_crid = row.Elements("field").First(f => f.Attribute("name").Value == "pcrid").Value, series_crid = row.Elements("field").First(f => f.Attribute("name").Value == "scrid").Value, programme_name = row.Elements("field").First(f => f.Attribute("name").Value == "title").Value, short_description = row.Elements("field").First(f => f.Attribute("name").Value == "description").Value, duration = Convert.ToInt32(row.Elements("field").First(f => f.Attribute("name").Value == "duration").Value), service_id = services.GetServiceByReduxId(row.Elements("field").First(f => f.Attribute("name").Value == "channel_id").Value).Id }; foreach (var item in items) { context.redux_items.AddObject(item); } context.SaveChanges(); }
private void ReadNewReduxDataStream(object sender, RoutedEventArgs e) { Services services = new Services(); redux_items item = null; var context = new ReduxEntities(); int count = 0; XmlReader reader = XmlReader.Create(@"D:\Docs\prog.txt"); while (reader.Read()) { if (reader.IsStartElement() && reader.Name == "row") { if (item != null) { if (context.redux_items.Any(i => i.disk_reference == item.disk_reference) == false) { //context.redux_items.AddObject(item); //count++; //if (count > 100) //{ // context.SaveChanges(); // context = new ReduxEntities(); // count = 0; //} } } item = new redux_items(); } /* * channel_id * disk_reference * title * description * duration * start * pcrid * scrid * end */ CultureInfo provider = CultureInfo.InvariantCulture; if (reader.IsStartElement() && reader.Name == "field") { var fieldname = reader.GetAttribute("name"); switch (fieldname) { case "channel_id": Service service = services.GetServiceByReduxId(reader.ReadElementContentAsString()); item.service_id = service.Id; break; case "disk_reference": item.disk_reference = reader.ReadElementContentAsString(); break; case "title": item.programme_name = reader.ReadElementContentAsString(); break; case "description": item.short_description = reader.ReadElementContentAsString(); break; case "duration": item.duration = reader.ReadElementContentAsInt(); break; case "start": string dt = reader.ReadElementContentAsString(); item.aired = DateTime.Parse(dt, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal); break; case "pcrid": item.programme_crid = reader.ReadElementContentAsString(); break; case "scrid": item.series_crid = reader.ReadElementContentAsString(); break; case "end": break; default: break; } } } if (item != null) { //context.redux_items.AddObject(item); //context.SaveChanges(); } }