XMLSerialization - appending blocks to existing xml file

F

Frank

Hi,

Let's say I have a file named myFile.xml

Within that file I have blocks of data which I'd like to add at different
times during the day.

e.g.

<LogEntry>
<Stuff> stuff here </Stuff>
<LogEntryDate>date time here</LogEntryDate>
</LogEntry>
....
<LogEntry>
<Stuff> stuff here </Stuff>
<LogEntryDate>date time here</LogEntryDate>
</LogEntry>


Now, I'd like to use XMLSerialization, but when appending I get the <xml...>
line which I don't want after the first entry.

<?xml version="1.0" encoding="utf-8"?>

Is there a way to use XMLSerializer so that when appending, this line is not
added?

Here is my current code, which creates the file if it doesn't exist,
otherwise appends the next chunk of xml.

XmlSerializer ser = new XmlSerializer(typeof(MyClass));


bool bAppend = true;

TextWriter writer = new StreamWriter(strUsageLogFileName, bAppend );

ser.Serialize(writer, this);

writer.Close();



Thanks for any tips,

Frank
 
S

sloan

You want to serialize a Collection of objects.

Then , unforunately, you'll have to:

Unserialize the xml to the collection.
Collection.Add(new objectOfYourType);
Serialize and Save.

See some collection xml serialization at:

http://sholliday.spaces.live.com/blog/
9/21/2005
XmlSerialization with IDictionary and CollectionBase Objects
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top