我有方法,它将创建对象,然后发送到另一个方法,它将执行一些操作,最后它被序列化为一个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
答案 0 :(得分:1)
每次调用sw = new StreamWriter(ReservationFilename);
时,StreamWriter都会覆盖以前写在文件上的所有数据。使用FileStream
对象,您可以决定以Append
模式打开文件。
此外,请务必使用StreamWriter
块后使用using
处置{/ 1}}:
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