user6875449 user6875449 - 1 month ago 17
C# Question

Serializing objects into xml

I have methods, that will create objects, then sent to another method which will do some operations, and finally it gets serialized into an xml file. the problem. the xml only has the last object that was created, it is missing the first 15 of them.

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

Answer

Each time you call sw = new StreamWriter(ReservationFilename); the StreamWriter overwrite all the data that was previously written on the file. With the FileStream object you can decide to open your file in Append mode.

Also, remember to dispose your StreamWriter after using it, using the using block:

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 
Comments