sider

hi everyone,
if have a project of a Car Rent, with serializable XML Roots (CarGroup, Car, .. and therefore fields and methods like GetCar, ...), so the the three looks like:

-CarData
-CarGroup
-Car
-CarCategorie

In my mainform i have a button for start renting a car, wich include following code fragment:

try
{
CarClass.CarData carDataSave = new CarClass.CarData();
CarClass.Rent rentDataSave = new CarClass.Rent();
foreach (ListViewItem item in listViewAvailableCarsForRent.Items)
{
CarClass.Car carSource = item.Tag as CarClass.Car;
if (carSource != null)
{
if (carSource.Categorie != null && carSource.Categorie.Length > 0)
{
for (int i = 0; i< carSource.Categorie.Length; i++)
{
if (carSource.CategorieIdea.IsAvailabe)
{
CarClass.CarGroup carGroupSource = item.Group.Tag as CarClass.CarGroup;
if (sourceGroup != null)
{
CarClass.CarGroup carGroupTarget =
carDataSave.GetCarGroup(carGroupSource.Id, carGroupSource.Type);
CarClass.Car carTarget = carGroupTarget.GetCar(carSource.Id, carSource.Name);
CarClass.CarCategorie carCategorie =
carTarget.GetCarCategorie(carSource.CategorieIdea.Id,
carSource.CategorieIdea.Name);
CarClass.Rent rentFile = rentDataSave.GetRentFile(id, day, name, begin, end,
insurance);
}

XmlSerializer rentSerializer = new XmlSerializer(typeof(CarClass.Rent));
System.IO.StreamWriter rentWrite = new System.IO.StreamWriter("RentFile.xml");
rentSerializer.Serialize(rentWrite, rentDataSave);
rentWrite.Flush();
rentWrite.Close();
}
}
}
}
}

catch....

My Problem is that by clicking the button every time only one set of (rent-)data is storing in the "RentFile.xml"-File...

What can I do to add a new data set to the old store ones in the xml file every time I press the button....

Please help!

Thanks in advance,
best regards bg


Re: XML and the .NET Framework Problems with XML Serializer

Gregor Berginc

Perhaps you could initialize your StreamWriter to append data:

StreamWriter rentWrite = new StreamWriter("RentFile.xml", true);

HTH,





Re: XML and the .NET Framework Problems with XML Serializer

glasgow1

hello Gregor,
thanks for your quick answer!!

I tried your code, but I¡¯m getting an error for the code line (*) in the following code fragment:

private void LoadRentFiles()
{
XmlSerializer rentSerializer = new XmlSerializer(typeof(CarClass.Rent));
StreamReader rentReader = new StreamReader("rentFile.xml", Encoding.UTF8);
* CarClass.Rent rentFiles = (CarClass.Rent)rentSerializer.Deserialize(rentReader);
rentReader.Close();
.....




Re: XML and the .NET Framework Problems with XML Serializer

Gregor Berginc

Ah, you need to deserialize that file. The problem is that now you have several valid xml documents in your file but that file cannot be deserialized as CarClass.

I doubt you can specify a "region" of a file you would like to deserialize. Do you need to append all these XMLs to a single file What if you use one file per document

Best,





Re: XML and the .NET Framework Problems with XML Serializer

glasgow1

The problem ist, that the xml-file is not well-formed (so I get the following error):

XML-Error: "Junk" after Document-Element
Adress: file:///C:/projects/CarRent/CarRent/bin/Debug/rentFile.xml
Line Nr. 9, Column 8:</Rent>< xml version="1.0" encoding="utf-8" >
----------^

my xml-file:

<Rent>
<Rents>
<Rent Id="1" Day="2007-06-21T09:19:07.503125+02:00" Name="Max Mustermann">
<Begin>2007-06-21T08:30:00</Begin>
<End>2007-06-21T09:30:00</End>
<Insurance>Yes</Insurance>
</Rent>
</Rents>
</Rent>

Any ideas




Re: XML and the .NET Framework Problems with XML Serializer

Gregor Berginc

Stupid solution would be to first deserialize your Rents based XML file, add new rent and save the file again...

Another would be to serialize new Rent to a stream and add it to the rentFile.xml manually. If you need to maintain the order, then you would need to find a line, containing </Rents>, insert serialized xml and close the file, i.e., add </Rents></Rent>.

Sorry for not being able to help you more,