将对象序列化为 xml

Serializing objects into xml

我有一些方法,可以创建对象,然后发送到另一个方法,该方法将执行一些操作,最后它被序列化到一个 xml 文件中。问题。 xml 只有最后创建的对象,缺少前 15 个对象。

public void reserveRoom()
    {
        reserveRoom("2345", "20160905", 1, "00001", "KB");
        reserveRoom("2345", "20160905", 4, "00002", "KB");
        reserveRoom("2345", "20160905", 5, "00003", "KB");
        reserveRoom("2345", "20160907", 3, "00004", "KB");
        reserveRoom("2345", "20160909", 4, "00005", "KB");
        reserveRoom("2345", "20160906", 5, "00006", "KB"); // fails - room not available
        reserveRoom("2345", "20160905", 1, "00007", "QB");
        reserveRoom("2345", "20160905", 1, "00008", "KB"); // fails- room not available
        reserveRoom("2345", "20160905", 4, "00009", "QB");
        reserveRoom("2345", "20170905", 1, "00010", "KB"); // fails- room not available
        reserveRoom("7890", "20160915", 5, "00011", "QB");
        reserveRoom("7890", "20160925", 10, "00012", "QB"); // fails- room not available
        reserveRoom("7890", "20160907", 3, "00013", "QB");
        reserveRoom("7890", "20160909", 3, "00014", "KB");
        reserveRoom("7890", "20160905", 1, "00015", "AB"); // fails- unknown room type
        reserveRoom("1234", "20160905", 1, "00016", "KB"); 
    }

public void reserveRoom(string h, string d, int n, string c, string t)
    {
        ReservationType reservation = new ReservationType();

        reservation.hotelId = h;
        reservation.StartDate = d;
        reservation.numDays = n;
        reservation.customerId = c;
        reservation.roomType = t;

       // Console.Write(reservation.customerId + ": " + Environment.NewLine);

        bool result = ReserveRoom(reservation);


    }

public bool ReserveRoom(ReservationType reservation)
    {
        ReservationType RequestReservation = new ReservationType();
        RequestReservation = reservation;   //List that contains the test cases.
        string reservationid = "0001";
        int number = int.Parse(reservationid);
        int count = 0;

        List<String> DateList = CreateDateList(RequestReservation.StartDate, RequestReservation.numDays);

            foreach (Inventory inventory in RoomInventory)
            {
                if (RequestReservation.hotelId == inventory.HotelId)
                  {

                    if (RequestReservation.roomType == inventory.RoomType)
                    {
                        for (int i = 0; i < DateList.Count; i++)
                        {
                            if (DateList[i] == inventory.Date && inventory.Quantity > 0)
                            {
                                 count++;
                                 inventory.Quantity--;
                                if (RequestReservation.numDays == count)
                                {
                                    RequestReservation.reservationId = "000"+(number + 1).ToString();
                                    RequestReservation.result = ReservationType.ReservationResultType.Success;
                                    count = 0;


                                foreach (Hotels h in LHotels)
                                    {
                                        foreach (Room rm in h.RoomList)
                                        {
                                            if (RequestReservation.hotelId == h.HotelId && RequestReservation.roomType == rm.RoomType)
                                            {

                                                RequestReservation.cost = (RequestReservation.numDays * rm.Rate);
                                            }                                               
                                        }
                                    }

                               } // 4th if , numDays == Count

                                else 
                                {                                      
                                    RequestReservation.reservationId = null;
                                    RequestReservation.result = ReservationType.ReservationResultType.RoomNotAvailable;                                     
                                }

                           } //3rd if,  DateList check

                        }//Forloop, Datelist

                    }// 2nd if, roomtype check

                }//first if ,hotelId check

            number++;

        } // Inventory List Foreach loop



                serial = new XmlSerializer(RequestReservation.GetType());
                sw = new StreamWriter(ReservationFilename);
                serial.Serialize(sw, RequestReservation);
                sw.Close();

        return true;
    }// End of ReserveRoom Method

每次调用 sw = new StreamWriter(ReservationFilename); StreamWriter 都会覆盖之前写入文件的所有数据。使用 FileStream 对象,您可以决定以 Append 模式打开文件。

此外,请记住在使用后使用 using 块处理您的 StreamWriter

serial = new XmlSerializer(RequestReservation.GetType());

using (FileStream fs = new FileStream(ReservationFilename, FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
    serial.Serialize(sw, RequestReservation); // Serialize at the end of the file
} // Closing and disposing FileStream and StreamWriter