473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
4 2187
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
> 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
>> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ellixis | last post by:
Hello, How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into...
9
by: Sarath | last post by:
I am working with an application using ASP, getting below error when i am trying to include new asp include file. _____________________________________________________________________ Microsoft...
7
by: Bob | last post by:
Hi, I am trying to use BULK INSERT with format file. All of our data has few bytes of header in the data file which I would like to skip before doing BULK INSERT. Is it possible to write...
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to...
2
by: Rachel | last post by:
Hi All: I am having a problem with visual studio automatically erasing my code. I thought it was a fluke at first, but it KEEPS HAPPENING. I have a string variable for a data connection that I...
5
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I'm drawing text using the DrawString method. I need a way to go back and erase one or more characters. As a first thought it seemed that one way to do it would be to go back to the character(s)...
6
by: Homer J. Simpson | last post by:
Hi all, I have enough experience with HTML/classic ASP to get by, and I'm trying to learn ASP.NET. Traditionally, I've taken the habit of breaking out extra-long CSS files into multiple,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.