D
darrel
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
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