public async Task <ActionResult> SaveConfiguration(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    using (var ms = new MemoryStream())
                    {
                        await file.InputStream.CopyToAsync(ms);

                        ms.Position = 0;

                        var contents   = await new StreamReader(ms).ReadToEndAsync();
                        var serializer = new XmlSerializer(typeof(ParametersMachineModelXml));
                        using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(contents)))
                        {
                            var           resultingMessage = (ParametersMachineModelXml)serializer.Deserialize(memStream);
                            List <string> errors           = new List <string>();
                            if (ValidateLolaXml(resultingMessage, out errors))
                            {
                                await _xmlDataService.AddOrUpdateMachineParameterAsync(resultingMessage);
                            }
                            else
                            {
                                ViewBag.success = false;
                                ViewBag.errors  = "File non valido";
                                if (errors.Count > 0)
                                {
                                    foreach (var ee in errors)
                                    {
                                        ViewBag.errors += $"\\n {ee}";
                                    }
                                }
                                //throw new Exception("Validazione LOLA.xml Fallita");
                                return(View("Index"));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var errMessage = string.Format(ex.GetStringLog());
                    LogService.WriteLog(errMessage, LogService.TypeLevel.Error, ex);
                    ViewBag.success = false;
                    ViewBag.errors  = "Caricamento XML Fallito \\n" + ex.Message;
                    return(View("Index"));
                }
            }
            else
            {
                ViewBag.success = false;
                ViewBag.errors  = "Caricamento XML Fallito \\n File non trovato";
                return(View("Index"));
            }
            ViewBag.success = true;
            return(View("Index"));
        }
Esempio n. 2
0
        private static void DeserializeObject(string filename)
        {
            Console.WriteLine($"{filename} - Start");
            // Create an instance of the XmlSerializer.
            XmlSerializer serializer =
                new XmlSerializer(typeof(ParametersMachineModelXml));

            // Declare an object variable of the type to be deserialized.
            ParametersMachineModelXml i;

            using (Stream reader = new FileStream(filename, FileMode.Open))
            {
                // Call the Deserialize method to restore the object's state.
                i = (ParametersMachineModelXml)serializer.Deserialize(reader);
            }

            _xmlDataService.AddOrUpdateMachineParameterAsync(i).Wait();
            Console.WriteLine($"{filename} - End");
        }
        public async Task <IHttpActionResult> Load(ParametersMachineModelXml pmm)
        {
            await _xmlDataService.AddOrUpdateMachineParameterAsync(pmm);

            return(Ok());
        }