Results 1 to 6 of 6

Thread: [RESOLVED] Updating a shared XML file

  1. #1

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    Resolved [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:
    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
    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 ...

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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...

  3. #3

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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 ...

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  5. #5

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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 ...

  6. #6

    Thread Starter
    Fanatic Member Mr.No's Avatar
    Join Date
    Sep 2002
    Location
    Mauritius
    Posts
    651

    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:
    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.SetLength(0)   ' This clears any previous data from the file
    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
    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
  •  



Click Here to Expand Forum to Full Width