private void LoadMatrix(object param) { string str = "txt"; string filter = "GCS Skill files (*." + str + ")| *." + str + "| All Files(*.*) | *.* "; OpenFileDialog dlg = new OpenFileDialog(); dlg.InitialDirectory = @"C:\Users\Derdan\Dropbox\Work\Matrix"; dlg.Filter = filter; dlg.ShowDialog(); if (dlg.FileName != "") { matrixFilePatch = dlg.FileName; NotifyPropertyChanged("MatrixFilePatch"); Encoding enc = Encoding.GetEncoding(1251); string[] stringFromFile = File.ReadAllLines(matrixFilePatch, enc); StringBuilder strResult = new StringBuilder(); foreach (var itemStringFromFile in stringFromFile) { strResult.Append(itemStringFromFile); strResult.Append('\n'); } var date = Regex.Match(strResult.ToString(), @"(?<=Дата и время взвешивания: )(.*)"); var dateee = Convert.ToDateTime(date.ToString()); var mtrNumVar = Regex.Match(strResult.ToString(), @"(?<=Номер взвешивания:)\W\d*(?=;)").ToString(); int mtrNum = Convert.ToInt32(mtrNumVar); var matrixContext = context.MatrixxDbSet.FirstOrDefault(p => p.MatrixNum == mtrNum); if (matrixContext != null) { matrixContext.MatrixDate = dateee; matrixContext.MatrixType = (string)param; var vagonsFromMatrix = Regex.Matches(strResult.ToString(), @":.*:.*:.*:.*:"); foreach (var item in vagonsFromMatrix) { var spr = item.ToString().Split(':'); int vgnNum = Convert.ToInt32(spr[1]); int?vagnum = null; if (Regex.IsMatch(spr[3], @"\d{8}")) { vagnum = Convert.ToInt32(spr[3]); } var matrWgn = matrixContext.MatrixWagons.First(p => p.WagonNumberPP == vgnNum); matrWgn.WagonNumberMatrix = vagnum; matrWgn.WagonNumberPP = Convert.ToInt32(spr[1]); matrWgn.Speed = spr[2]; matrWgn.Weight = spr[4]; } Matrix = matrixContext; NotifyPropertyChanged("Matrix"); context.SaveChanges(); } else { Matrixx mtr = new Matrixx(); mtr.MatrixDate = dateee; mtr.MatrixNum = mtrNum; mtr.MatrixType = (string)param; var vagons = Regex.Matches(strResult.ToString(), @":.*:.*:.*:.*:"); foreach (var item in vagons) { var spr = item.ToString().Split(':'); MatrixWagon mtrwgn = new MatrixWagon(); mtrwgn.WagonNumberPP = Convert.ToInt32(spr[1]); mtrwgn.Speed = spr[2].Trim(); mtrwgn.Weight = spr[4].Trim(); int vagnum; if (Regex.IsMatch(spr[3], @"\d{8}")) { vagnum = Convert.ToInt32(spr[3]); Wagon insertWag = context.WagonDbSet.FirstOrDefault(p => p.Nwag == vagnum); if (insertWag == null) { Wagon wags = new Wagon { Nwag = Convert.ToInt32(vagnum), Ownerc = string.Empty, Gp = string.Empty, Tara = string.Empty }; context.WagonDbSet.Add(wags); } mtrwgn.WagonNumberMatrix = vagnum; } mtr.MatrixWagons.Add(mtrwgn); } Matrix = mtr; NotifyPropertyChanged("Matrix"); context.MatrixxDbSet.Add(mtr); context.SaveChanges(); } } }
public SmgsSeriallXML(string filePatch) { ShFluContext context = new ShFluContext(); string[] stringFromFile = File.ReadAllLines(filePatch); StringBuilder strResult = new StringBuilder(); foreach (var itemStringFromFile in stringFromFile) { strResult.Append(itemStringFromFile); strResult.Append('\n'); } string commentPattern3 = @"\<\?xml(\w|\W)*</doc>"; var reg = Regex.Match(strResult.ToString(), commentPattern3).ToString(); XmlDocument doc = new XmlDocument(); doc.LoadXml(reg); XDocument xdoc = XDocument.Parse(doc.OuterXml); SmgsNakl smgsNak = null; foreach (XElement smgsXElement in xdoc.Root.Elements("table")) { if (smgsXElement.FirstAttribute.Value == "nakl") { //smgsNak.Smgs = Convert.ToInt32(smgsXElement.Element("data").Element("smgs").Value); var smgs = Convert.ToInt32(smgsXElement.Element("data").Element("smgs").Value); smgsNak = context.SmgsNaklDbSet.FirstOrDefault(p => p.Smgs == smgs); if (smgsNak == null) { smgsNak = new SmgsNakl(); smgsNak.Smgs = Convert.ToInt32(smgsXElement.Element("data").Element("smgs").Value); smgsNak.Smgsdat = Convert.ToDateTime(smgsXElement.Element("data").Element("smgsdat").Value); smgsNak.mnet = smgsXElement.Element("data").Element("mnet").Value; smgsNak.mbrt = smgsXElement.Element("data").Element("mbrt").Value; } } if (smgsXElement.FirstAttribute.Value == "nakl_gruz") { smgsNak.Etsngn = smgsXElement.Element("data").Element("etsngn").Value; smgsNak.gngc = smgsXElement.Element("data").Element("gngc").Value; smgsNak.gngn = smgsXElement.Element("data").Element("gngn").Value; smgsNak.etsngc = smgsXElement.Element("data").Element("etsngc").Value; } if (smgsXElement.FirstAttribute.Value == "nakl_vag") { foreach (var vagonItem in smgsXElement.Elements("data")) { int vagnum = Convert.ToInt32(vagonItem.Element("nwag").Value); var insertWag = context.WagonDbSet.FirstOrDefault(p => p.Nwag == vagnum); if (insertWag == null) { Wagon wag = new Wagon { Nwag = Convert.ToInt32(vagonItem.Element("nwag").Value), Ownerc = vagonItem.Element("ownerc").Value, Gp = vagonItem.Element("gp").Value, Tara = vagonItem.Element("tara").Value }; context.WagonDbSet.Add(wag); } else { insertWag.Nwag = Convert.ToInt32(vagonItem.Element("nwag").Value); insertWag.Ownerc = vagonItem.Element("ownerc").Value; insertWag.Gp = vagonItem.Element("gp").Value; insertWag.Tara = vagonItem.Element("tara").Value; } if (smgsNak.SmgsId == 0) { WagInSmgs wgs = new WagInSmgs(); wgs.Wagon = context.WagonDbSet.Local.FirstOrDefault(p => p.Nwag == vagnum); wgs.Tarapr = vagonItem.Element("tarapr").Value; wgs.Weightb = vagonItem.Element("weightb").Value; wgs.Weight = vagonItem.Element("weight").Value; smgsNak.WagInSmgses.Add(wgs); } else { WagInSmgs wgs = smgsNak.WagInSmgses.First(p => p.Wagon.Nwag == vagnum); wgs.Wagon = context.WagonDbSet.Local.FirstOrDefault(p => p.Nwag == vagnum); wgs.Tarapr = vagonItem.Element("tarapr").Value; wgs.Weightb = vagonItem.Element("weightb").Value; wgs.Weight = vagonItem.Element("weight").Value; } } } } if (smgsNak.SmgsId == 0) { context.SmgsNaklDbSet.Add(smgsNak); } context.SaveChanges(); }
private void Delete(object param) { AllSmgsNakl.Remove((SmgsNakl)param); context.SmgsNaklDbSet.Remove((SmgsNakl)param); context.SaveChanges(); }
private void Save() { context.SaveChanges(); }