XMLSerialization - appending blocks to existing xml file

Discussion in 'ASP .Net' started by Frank, Mar 5, 2007.

  1. Frank

    Frank Guest

    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
    Frank, Mar 5, 2007
    #1
    1. Advertising

  2. Frank

    sloan Guest

    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




    "Frank" <> wrote in message
    news:45eca789$0$27061$...
    > 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
    >
    >
    sloan, Mar 6, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QSBwcm9ncmFtbWVyIGRlc3BlcmF0bHkgbmVlZGlu

    XMLSerialization: Sometimes csc.exe freezes on the PermissionSet

    =?Utf-8?B?QSBwcm9ncmFtbWVyIGRlc3BlcmF0bHkgbmVlZGlu, Nov 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    619
    =?Utf-8?B?QSBwcm9ncmFtbWVyIGRlc3BlcmF0bHkgbmVlZGlu
    Nov 18, 2004
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    XmlSerialization Problem

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Nov 28, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    =?Utf-8?B?UmFlZCBTYXdhbGhh?=
    Nov 28, 2004
  3. No one

    XmlSerialization error

    No one, Jun 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    398
    No one
    Jun 18, 2005
  4. Replies:
    8
    Views:
    445
  5. matt
    Replies:
    1
    Views:
    238
    George Ogata
    Aug 6, 2004
Loading...

Share This Page