Hi

I have XML file on a server that can be updated by many client workstations. So my code looks lie this:

VB Code:
  1. Public sub UpdateControl()
  2.     Dim Doc As New XmlDocument
  3.     Dim fs As FileStream
  4.  
  5.     Try
  6.       fs = New FileStream(_BatchListFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
  7.       ' Load the XML through the filestream
  8.       Doc.Load(fs)
  9.  
  10.       ' Pick up node using SelectSingleNode and update
  11.  
  12.       fs.position = 0
  13.      ' Write back the Xml file
  14.       Doc.Save(fs)
  15.     Catch ex as Exception
  16.       throw new Exception("", ex)
  17.     Finally
  18.        if not(fs is nothing) Then
  19.          fs.close
  20.          fs = nothing
  21.        End If
  22.     End Try
  23. 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:
  1. <?xml version="1.0" encoding="utf-8">
  2. <root>
  3. <customer>
  4.   <surname>Very Long Surname</surname>
  5. </customer>
  6. <customer>
  7. ' more customer data
  8. </customer>
  9. </root>

Updated Xml. surname innertext reduced following removal of Long
VB Code:
  1. <?xml version="1.0" encoding="utf-8">
  2. <root>
  3. <customer>
  4.   <surname>Very Surname</surname>
  5. </customer>
  6. <customer>
  7. ' more customer data
  8. </customer>
  9. </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