protected void btnSave_Click(object sender, EventArgs e) { //creating a restaurants object from the session itineraries itneraryList = null; itneraryList = Session["itneraryListSession"] as itineraries; if (drpPassenger.SelectedValue != "0") //if there is a restaurant selected { //create a restaurant object for the selected restaurant and displayin its info itinerariesItinerary iti = itneraryList.itinerary[drpPassenger.SelectedIndex - 1]; iti.outbound.departure.city = txtOutboundDeparture.Text; iti.outbound.arriving.city = txtOutboundArriving.Text; [email protected] = txtReturnDeparture.Text; [email protected] = txtReturnArriving.Text; //creating a string from the xml file string xmlFile = MapPath(@"~/itineraries.xml"); // serializing (converting from object to xml) the file: using (FileStream xs = new FileStream(xmlFile, FileMode.Create)) { XmlSerializer serializor = new XmlSerializer(typeof(itineraries)); serializor.Serialize(xs, itneraryList); } //showing the confirmation message saying that the file has been saved lblConfirmation.Visible = true; lblConfirmation.Text = "Revised Itinerary has been saved to: <br/>" + xmlFile; } }
protected void drpPassenger_SelectedIndexChanged(object sender, EventArgs e) { //Add your code to handle the event when the user selects a different passenger //creating a list of restaurants from the data stored in the session from xml file itineraries itneraryList = Session["itneraryListSession"] as itineraries; //displaying additional fields when there's something selected from dropdown list: if (drpPassenger.SelectedValue == "0") { txtOutboundDeparture.Visible = false; txtOutboundArriving.Visible = false; txtReturnDeparture.Visible = false; txtReturnArriving.Visible = false; return; } else { txtOutboundDeparture.Visible = true; txtOutboundArriving.Visible = true; txtReturnDeparture.Visible = true; txtReturnArriving.Visible = true; //calling function to display info on itineraries: displayItineraryInfo(itneraryList.itinerary[drpPassenger.SelectedIndex - 1]); } }
protected void Page_Load(object sender, EventArgs e) { //Add your code to deserialize the xml file and initialize the dropdown list. itineraries itneraryList = null; if (Session["itneraryListSession"] == null) //in case there is no session yet { //opening the xml file string xmlFile = MapPath(@"~/itineraries.xml"); using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { //deserializing the file (converting it into an object) XmlSerializer serial = new XmlSerializer(typeof(itineraries)); itneraryList = (itineraries)serial.Deserialize(xs); } Session["itneraryListSession"] = itneraryList; } else { itneraryList = Session["itneraryListSession"] as itineraries; } if (!IsPostBack) //display restaurant list using DataBinding: { drpPassenger.DataSource = itneraryList.itinerary; drpPassenger.DataTextField = "passenger"; drpPassenger.DataBind(); drpPassenger.Items.Insert(0, new ListItem("Select One...", "0")); } }
protected void drpPassenger_SelectedIndexChanged(object sender, EventArgs e) { string xmlFile = MapPath(@"~/App_Data/itineraries.xml"); //Add your code to handle the event when the user selects a different passenger //Deserialize the XML (in each step) itineraries allItineraries = null; using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { XmlSerializer serial = new XmlSerializer(typeof(itineraries)); allItineraries = (itineraries)serial.Deserialize(xs); } //Remove all fields when index 0 is selected if (drpPassenger.SelectedValue == "0") { txtOutboundDeparture.Visible = false; txtOutboundArriving.Visible = false; txtInboundDeparture.Visible = false; txtInboundArriving.Visible = false; return; } else { //fill in relevant fields with data txtOutboundDeparture.Visible = true; txtOutboundArriving.Visible = true; txtInboundDeparture.Visible = true; txtInboundArriving.Visible = true; itinerariesItinerary info = allItineraries.itinerary[drpPassenger.SelectedIndex - 1]; txtOutboundDeparture.Text = info.outbound.departure.city; txtOutboundArriving.Text = info.outbound.arriving.city; txtInboundDeparture.Text = info.inbound.departure.city; txtInboundArriving.Text = info.inbound.arriving.city; drpPassenger.SelectedValue = info.passenger.ToString(); } }
public static List <TripItinerary> GetItinerariesFromXml() { lock (fileLock) { try { itineraries allItinearies = null; string xmlFile = HostingEnvironment.MapPath(@"~/App_Data/itineraries.xml"); using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { XmlSerializer serializor = new XmlSerializer(typeof(itineraries)); allItinearies = (itineraries)serializor.Deserialize(xs); } List <TripItinerary> itins = allItinearies.itinerary.Select <itinerariesItinerary, TripItinerary>(MapItineray).ToList(); return(itins); } catch { return(new List <TripItinerary>()); } } }
protected void btnSave_Click(object sender, EventArgs e) { string xmlFile = MapPath(@"~/App_Data/itineraries.xml"); lblConfirmation.Visible = true; lblConfirmation.Text = "Revised Itinerary has been saved to <br/>" + xmlFile; //Deserialize the XML (in each step) itineraries allItineraries = null; using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { XmlSerializer serial = new XmlSerializer(typeof(itineraries)); allItineraries = (itineraries)serial.Deserialize(xs); } //If a passenger is presently selected if (drpPassenger.SelectedValue != "0") { itinerariesItinerary info = allItineraries.itinerary[drpPassenger.SelectedIndex - 1]; info.outbound.departure.city = txtOutboundDeparture.Text; info.outbound.arriving.city = txtOutboundArriving.Text; info.inbound.departure.city = txtInboundDeparture.Text; info.inbound.arriving.city = txtInboundArriving.Text; // serializing the xml to write changes using (FileStream xs = new FileStream(xmlFile, FileMode.Create)) { XmlSerializer serializor = new XmlSerializer(typeof(itineraries)); serializor.Serialize(xs, allItineraries); } //Add confirmation message lblConfirmation.Visible = true; lblConfirmation.Text = "Revised Itinerary has been saved to: <br/>" + xmlFile; } }
protected void Page_Load(object sender, EventArgs e) { string xmlFile = MapPath(@"~/App_Data/itineraries.xml"); lblConfirmation.Visible = false; //Add your code to deserialize the xml file and initialize the dropdown list. if (!IsPostBack) // { //Deserialize the XML (in each step) itineraries allItineraries = null; using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { XmlSerializer serial = new XmlSerializer(typeof(itineraries)); allItineraries = (itineraries)serial.Deserialize(xs); } drpPassenger.DataSource = allItineraries.itinerary; drpPassenger.DataTextField = "passenger"; drpPassenger.DataBind(); drpPassenger.Items.Insert(0, new ListItem("Select One...", "0")); } }
public static string SaveItinerary(TripItinerary newItinerary) { if (newItinerary == null) { return("No itinerary received!"); } itinerariesItinerary it = new itinerariesItinerary(); if (string.IsNullOrWhiteSpace(newItinerary.PassengerName)) { return("Passenger name missing!"); } it.passenger = newItinerary.PassengerName; if (!cities.Contains(newItinerary.DepartureCity)) { return("Departure city is not valid!"); } if (!cities.Contains(newItinerary.ArrivingCity)) { return("Arriving city is not valid!"); } it.outbound = new itinerariesItineraryOutbound(); it.outbound.departure = new itinerariesItineraryOutboundDeparture(); it.outbound.departure.city = newItinerary.DepartureCity; it.outbound.arriving = new itinerariesItineraryOutboundArriving(); it.outbound.arriving.city = newItinerary.ArrivingCity;; if (string.IsNullOrWhiteSpace(newItinerary.Date)) { return("Trip date missing!"); } try { it.outbound.departure.date = DateTime.ParseExact(newItinerary.Date, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); it.outbound.arriving.date = it.outbound.departure.date; } catch { return("Trip date is not a valid date or is in wrong format!"); } string xmlFile = HostingEnvironment.MapPath(@"~/App_Data/itineraries.xml"); lock (fileLock) { try { itineraries allItinearies = null; using (FileStream xs = new FileStream(xmlFile, FileMode.Open)) { XmlSerializer serializor = new XmlSerializer(typeof(itineraries)); allItinearies = (itineraries)serializor.Deserialize(xs); } List <itinerariesItinerary> itineraryList = allItinearies.itinerary.ToList(); itineraryList.Add(it); allItinearies.itinerary = itineraryList.ToArray(); using (FileStream xs = new FileStream(xmlFile, FileMode.Create)) { XmlSerializer serializor = new XmlSerializer(typeof(itineraries)); serializor.Serialize(xs, allItinearies); } return("New itineray has been saved."); } catch (Exception e) { return("Unable to save new itinerary: " + e.Message); } } }