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;
        }
    }
 //function to display info on itineraries:
 public void displayItineraryInfo(itinerariesItinerary iti)
 {
     txtOutboundDeparture.Text  = iti.outbound.departure.city;
     txtOutboundArriving.Text   = iti.outbound.arriving.city;
     txtReturnDeparture.Text    = [email protected];
     txtReturnArriving.Text     = [email protected];
     drpPassenger.SelectedValue = iti.passenger.ToString();
 }
Beispiel #3
0
    private static TripItinerary MapItineray(itinerariesItinerary arg)
    {
        TripItinerary itin = new TripItinerary();

        itin.PassengerName = arg.passenger;
        itin.DepartureCity = arg.outbound.departure.city;
        itin.ArrivingCity  = arg.outbound.arriving.city;
        itin.Date          = arg.outbound.departure.date.ToString("yyyy-MM-dd");
        return(itin);
    }
    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();
        }
    }
    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;
        }
    }
Beispiel #6
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);
            }
        }
    }