|
-
Apr 20th, 2006, 09:05 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Updating a shared XML file
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
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Apr 20th, 2006, 10:07 AM
#2
Re: Updating a shared XML file
why open the XML doc from a filestream? the XmlDocument object can load directly from a file..
XML is nothing more than a text file in a specific format.... when you read in an XML document, using the xmldocument.load method.. it is read and closed immediatly... the xmldocument has all the info about the xml from the file, but it is now independant of the file.. you need to call the xmldocument.save method to save any changes to the XML back to the file.. but since like I said its a text file.. it simply rewrites the xml file each time you save...
-
Apr 20th, 2006, 10:42 AM
#3
Thread Starter
Fanatic Member
Re: Updating a shared XML file
kleinma
The main reason to open the XML file through the filestream is to keep it locked while I'm updating it. Of course in my actual application I have a mechanism to retry when the file is locked by another client. Is there anyway I can lock the file so that while its loaded in the XmlDomDocument object someone else doesn't load it.
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Apr 20th, 2006, 10:49 AM
#4
Re: Updating a shared XML file
no there isn't because the XMLDoc is not connected to the actual XML file in any way.. it just reads the file or writes a file... the manipulation of the XML data in the XMLDocument is only being manipulated in the object itself.. not in the file... changes are never made until the save method is called...
I am not sure why you are getting a weird cutoff in your XML document.. I would probably need to see the code that does the actual XML manipulation between the load and the save in order to try to figure out whats going on
-
Apr 20th, 2006, 11:03 AM
#5
Thread Starter
Fanatic Member
Re: Updating a shared XML file
kleinma
Thanks for responding. As you can see thats why I'm using a stream to keep the file locked. I'll try a few things such as setting the length of the Filestream to 0 using the SetLength method before the save and see if it works.
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
-
Apr 21st, 2006, 09:03 AM
#6
Thread Starter
Fanatic Member
Re: Updating a shared XML file
Found the solution. I use fs.SetLength(0) to set the stream to empty and then I save back the XML into it.
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.SetLength(0) ' This clears any previous data from the file
' 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
Using VB.NET 2003/.NET 1.1/C# 2.0
http://del.icio.us/rajoo
Blow your mind, smoke gunpowder
Ashes to ashes, dust to dust
If God won't have you, the devil will. - Author unknown
Don't follow me, I'm lost too ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|