By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
May 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

May 5 '06 #2

P: n/a
> 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
May 5 '06 #3

P: n/a
>> 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
May 5 '06 #4

P: n/a
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.
May 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.