Proper way to write a CLEAN xml file (erasing old data first)

Discussion in 'ASP .Net' started by darrel, May 5, 2006.

  1. darrel

    darrel Guest

    I'm backtracking to a problem I had a month or so ago.

    I need to write XML files quite a bit. I'm finding that the way I'm doing it
    doesn't write a new, clean XML file each time, but just dumps the new data
    'on top' of the old stuff.

    If the new data is shorter than the old data, the remainder of the old data
    just sits at the end of the file making the XML file invalid.

    Example:

    if my existing data is:

    <items>
    <item></item>
    <item></item>
    </items>

    and my new data is:

    <items>
    <item></item>
    </items>

    What gets written is:

    <items>
    <item></item>
    </items>
    </items>

    I'm writing the XML using this:

    Dim fs As New System.IO.FileStream("myfile.xml"), IO.FileMode.Open,
    IO.FileAccess.Write, IO.FileShare.Read)
    objXMLWriter = New System.xml.XmlTextWriter(fs,
    System.Text.Encoding.Default)

    Should I be using something else? XMLWriter? Textwriter?

    Up to now, the 'fix' I've been using is to first delete the XML file. Create
    a new streamwriter, output it to a blank text file with the 'myfile.xml'
    name, and then doing the xml writer. But this seems like a hack. I'm
    guessing there is some method I should be using that will first delete the
    content of the xml file before writing the new content automatically.

    -Darrel
    darrel, May 5, 2006
    #1
    1. Advertising

  2. Use IO.FileMode.Create to truncate the file if it exists.

    darrel wrote:
    > I'm backtracking to a problem I had a month or so ago.
    >
    > I need to write XML files quite a bit. I'm finding that the way I'm doing it
    > doesn't write a new, clean XML file each time, but just dumps the new data
    > 'on top' of the old stuff.
    >
    > If the new data is shorter than the old data, the remainder of the old data
    > just sits at the end of the file making the XML file invalid.
    >
    > Example:
    >
    > if my existing data is:
    >
    > <items>
    > <item></item>
    > <item></item>
    > </items>
    >
    > and my new data is:
    >
    > <items>
    > <item></item>
    > </items>
    >
    > What gets written is:
    >
    > <items>
    > <item></item>
    > </items>
    > </items>
    >
    > I'm writing the XML using this:
    >
    > Dim fs As New System.IO.FileStream("myfile.xml"), IO.FileMode.Open,
    > IO.FileAccess.Write, IO.FileShare.Read)
    > objXMLWriter = New System.xml.XmlTextWriter(fs,
    > System.Text.Encoding.Default)
    >
    > Should I be using something else? XMLWriter? Textwriter?
    >
    > Up to now, the 'fix' I've been using is to first delete the XML file. Create
    > a new streamwriter, output it to a blank text file with the 'myfile.xml'
    > name, and then doing the xml writer. But this seems like a hack. I'm
    > guessing there is some method I should be using that will first delete the
    > content of the xml file before writing the new content automatically.
    >
    > -Darrel
    >
    >
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 5, 2006
    #2
    1. Advertising

  3. darrel

    darrel Guest

    > Use IO.FileMode.Create to truncate the file if it exists.

    Is there an advantage to that vs. just deleting and creating a new blank
    file?

    I find it odd that, by default, xmlWriter doesn't first clear the file
    before writing...seems to be standard practice for every application I've
    ever used. ;o)

    -Darrel
    darrel, May 5, 2006
    #3
  4. darrel

    darrel Guest

    >> Use IO.FileMode.Create to truncate the file if it exists.
    >
    > Is there an advantage to that vs. just deleting and creating a new blank
    > file?


    Oh. Duh! Nevermind!

    Yea, I was OPENING a file rather than CREATING it. Thanks!

    -Darrel
    darrel, May 5, 2006
    #4
  5. darrel wrote:
    >> Use IO.FileMode.Create to truncate the file if it exists.

    >
    > Is there an advantage to that vs. just deleting and creating a new blank
    > file?


    Less code.

    > I find it odd that, by default, xmlWriter doesn't first clear the file
    > before writing...seems to be standard practice for every application I've
    > ever used. ;o)


    Not odd at all. You specifically opened the stream for writing with the
    current content intact. The XmlWriter just writes to the stream that you
    supply to it.

    If you would have used the XmlWriter.Create method to open the stream,
    it would have used IO.FileMode.Create to open the file.
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 5, 2006
    #5
    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. Max Ischenko

    Proper clean up of the COM server

    Max Ischenko, Oct 28, 2003, in forum: Python
    Replies:
    0
    Views:
    262
    Max Ischenko
    Oct 28, 2003
  2. wolverine
    Replies:
    1
    Views:
    309
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    May 30, 2007
  3. Replies:
    8
    Views:
    499
  4. Eric
    Replies:
    4
    Views:
    144
  5. Replies:
    5
    Views:
    68
    Chris Angelico
    May 14, 2014
Loading...

Share This Page