Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

Can someone please help a novice?

I am using XMLTextReader to hold the screen position of a form. The StoreFormInformation sub is called in the FormClosed event and LoadFormInformation is called in the Load event of the form. This works fine for a couple of times (no fixed amount, it just seems to happen randomly) but then for no apparent reason the xml gets corrupted (See below - for some reason extra characters are appended to the closing root level line, in this case a '>'). Can anyone please give me a tip as to where I am going wrong.

Thanks in advance.

-----Created XML-----

XML
<?xml version="1.0" encoding="utf-8"?>
<DERMPAMappingTools_MapDetails>
  <XPos>684</XPos>
  <YPos>73</YPos>
</DERMPAMappingTools_MapDetails>>



-----My Code-----

VB
Public Sub StoreFormInformation(ByVal frm As Form)
        On Error GoTo eh

        ' Get the isolated store for this assembly
        Dim isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()

        ' Create or open a file at the top level of the store
        Dim stmWriter As IsolatedStorageFileStream = New IsolatedStorageFileStream("FormSettings.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, isf)

        'Create stream XML writer
        Dim xmlWriter As New XmlTextWriter(stmWriter, Encoding.UTF8)

        'Add values to writer
        xmlWriter.Formatting = Formatting.Indented
        xmlWriter.WriteStartDocument()
        xmlWriter.WriteStartElement(frm.Name)
        xmlWriter.WriteStartElement("XPos")
        xmlWriter.WriteString(frm.Location.X)
        xmlWriter.WriteEndElement()
        xmlWriter.WriteStartElement("YPos")
        xmlWriter.WriteString(frm.Location.Y)
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndDocument()

        xmlWriter.Flush()
        xmlWriter.Close()
        stmWriter.Close()
        isf.Close()
        Exit Sub

eh:     MsgBox("StoreFormInformation - " & Err.Description)

    End Sub

    Public Sub LoadFormInformation(ByVal frm As Form)
        On Error GoTo eh

        ' Get the isolated store for this assembly
        Dim isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()

        Dim StoreFileNames As String()
        Dim StoreFile As String
        StoreFileNames = isf.GetFileNames("FormSettings.xml")

        For Each StoreFile In StoreFileNames
            If StoreFile = "FormSettings.xml" Then
                'If the file does exist, then processing to read the XML file into a stream reader object occurs:
                Dim stmReader As New StreamReader(New IsolatedStorageFileStream("FormSettings.xml", FileMode.Open, isf))
                Dim xmlReader As New XmlTextReader(stmReader)

                'Find attribute relative to form
                xmlReader.ReadToFollowing(frm.Name)

                Dim xPos, yPos As Integer

                While xmlReader.Read()
                    Select Case xmlReader.Name
                        Case "XPos"
                            xPos = xmlReader.ReadString
                        Case "YPos"
                            yPos = xmlReader.ReadString
                    End Select
                End While

                frm.Location = New System.Drawing.Point(xPos, yPos)
                'Again, as in the btnSave Click event we clean up by closing the reader objects:

                xmlReader.Close()
                stmReader.Close()
                'Close off the file search loop and the IsolatedStorageFile object:

            End If
        Next
        isf.Close()

        Exit Sub

eh:     MsgBox("LoadFormInformation - " & Err.Description)
        isf = Nothing
       

    End Sub
Posted

1 solution

My guess would be that you need to recreate the file instead of open or create. You probably write over the existing data in the file. If the previous content of the xml file was like this (both numbers are 3 characters long):
XML
<?xml version="1.0" encoding="utf-8"?>
<DERMPAMappingTools_MapDetails>
  <XPos>684</XPos>
  <YPos>731</YPos>
</DERMPAMappingTools_MapDetails>


The new content would not completely overwrite the complete xml, leaving an extra > as a last extra character.

Good luck!
 
Share this answer
 
Comments
FFP@GC 2-Jul-12 11:51am    
Thank you, that was it. I delete the file and then create a new one each time FormClosed is fired and its going great. On a side note, I cannot seem to find the FileExists method of the IsolatedStorageFile class. I meet the system req's stated here http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.fileexists.aspx but it doesnt come up in the intellisense. Is this a bug?

Thanks again!
E.F. Nijboer 3-Jul-12 3:48am    
You can simply use FileMode.Create instead of FileMode.CreateOrOpen. FileMode.Create forces the file to be recreated so there is no need to test if it exists. This way a new file is always created.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900