public ActionResult UploadXml(HttpPostedFileBase SourceFile) { XmlImportResults result = new XmlImportResults(); try { if (SourceFile == null || SourceFile.ContentLength < 1 || SourceFile.FileName.IsEmpty()) { throw new ArgumentNullException("SourceFile", "No source file selected!"); } result = XmlService.UploadXml(SourceFile.FileName, SourceFile.InputStream); } catch (Exception e) { if (result == null) { result = new XmlImportResults(); } result.ErrorMessage = e.Message; result.Success = false; } return(View("Index", result)); }
public static XmlImportResults UploadXml(string filename, Stream xmlStream) { var result = new XmlImportResults(); result.Success = false; DeletePreviousImports(); var xmlDocument = new XmlDocument(); xmlDocument.Load(xmlStream); var import = new XmlImport { ImportDate = DateTime.Now, XmlFileName = filename, XmlData = XmlUtility.ToString(xmlDocument) }; var importid = import.Insert(); var menuitems = ParseXml(xmlDocument); foreach (var xmlMenuItem in menuitems) { xmlMenuItem.XmlImportId = Convert.ToInt32(importid); var menuid = xmlMenuItem.Insert(); XmlMenuItemXrefRespositorycs.Save(xmlMenuItem); result.MenuItemsUploaded++; foreach (var ingredient in xmlMenuItem.Ingredients) { ingredient.XmlImportMenuItemId = Convert.ToInt32(menuid); ingredient.Save(); XmlIngredientsXrefRepository.Save(ingredient, xmlMenuItem.ItemGuid, xmlMenuItem.ItemName); result.IngredientsUploaded++; } } result.Success = true; return(result); }