public IActionResult Edit(RestaurantEditViewModel rsVM) { restaurant_review restaurantList = null; string xmlFilePath = Path.GetFullPath("Data/restaurant_review .xml"); XmlSerializer serializor = new XmlSerializer(typeof(restaurant_review)); FileStream xs = new FileStream(xmlFilePath, FileMode.Open); restaurantList = (restaurant_review)serializor.Deserialize(xs); xs.Close(); string xmlFile = Path.GetFullPath("Data/restaurant_review .xml"); XmlWriterSettings settings = new XmlWriterSettings(); settings.CheckCharacters = true; settings.Encoding = Encoding.Unicode; settings.Indent = true; settings.NewLineOnAttributes = true; XmlWriter xw = XmlWriter.Create(xmlFile, settings); XmlSerializer serializer = new XmlSerializer(typeof(restaurant_review)); int i = (int)rsVM.Id - 1; restaurant_reviewRestaurant restaurant = restaurantList.restaurant[i]; restaurant.name = rsVM.Name; restaurant.address.city = rsVM.City; restaurant.address.PostalZipCode = rsVM.PostalZipCode; restaurant.address.ProvinceState = rsVM.ProvinceState; restaurant.address.StreetAddress = rsVM.StreetAddress; restaurant.reviews.review.summary = rsVM.Summary; restaurant.reviews.review.rating.Value = rsVM.Rating.ToString(); serializer.Serialize(xw, restaurantList); xw.Close(); return(RedirectToAction("Index")); }
public void SaveRestaurant(RestaurantInfo restInfo) { string xmlFile = HttpContext.Current.Server.MapPath("~/App_Data/restaurant_review.xml"); restaurant_review allRestaurants = GetRestaurantsFromXml(); //Get restaurant by id and save it on xml file restaurant_reviewRestaurant restToUpdate = allRestaurants.restaurant[restInfo.Id]; if (restToUpdate != null) { restToUpdate.name = restInfo.Name; restToUpdate.reviews.review.summary = restInfo.Summary; restToUpdate.reviews.review.rating.Value = restInfo.Rating.ToString(); restToUpdate.address.StreetAddress = restInfo.Location.Street; restToUpdate.address.city = restInfo.Location.City; restToUpdate.address.ProvinceState = (ProvinceType)Enum.Parse(typeof(ProvinceType), restInfo.Location.Province, true); restToUpdate.address.PostalZipCode = restInfo.Location.PostalCode; using (FileStream xs = new FileStream(xmlFile, FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(restaurant_review)); serializer.Serialize(xs, allRestaurants); } } }