Hi
I have XML file on a server that can be updated by many client workstations. So my code looks lie this:
VB Code:
Public sub UpdateControl() Dim Doc As New XmlDocument Dim fs As FileStream Try fs = New FileStream(_BatchListFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None) ' Load the XML through the filestream Doc.Load(fs) ' Pick up node using SelectSingleNode and update fs.position = 0 ' Write back the Xml file Doc.Save(fs) Catch ex as Exception throw new Exception("", ex) Finally if not(fs is nothing) Then fs.close fs = nothing End If End Try End Sub
My problem is that sometimes I end up with the updated XML having a trailing part of text which seems to happen when I update the innerText of a node and the text becomes shorter. For example
Original Xml
VB Code:
<?xml version="1.0" encoding="utf-8"> <root> <customer> <surname>Very Long Surname</surname> </customer> <customer> ' more customer data </customer> </root>
Updated Xml. surname innertext reduced following removal of Long
VB Code:
<?xml version="1.0" encoding="utf-8"> <root> <customer> <surname>Very Surname</surname> </customer> <customer> ' more customer data </customer> </root></root> ' <<<<<< trailing text in updated file
SO my question is:
Am I using the right method to update the file. I know I could delete the file and recreate it but this seems a bit dirty to me.
Secondly, what is the best way to update an existing file.
Thanks




Reply With Quote