XML错误随机发生 [英] XML Error happening randomely

查看:115
本文介绍了XML错误随机发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有人可以帮助新手吗?

我正在使用XMLTextReader来保存表单的屏幕位置.在FormClosed事件中调用StoreFormInformation子项,在表单的Load事件中调用LoadFormInformation.这可以正常工作几次(没有固定的数量,它似乎是随机发生的),但是没有明显的原因使xml损坏(请参阅下文-由于某些原因,在结束的根级别行上附加了额外的字符)大小写为``>'').谁能给我一个小窍门,告诉我我要去哪里错了.

提前谢谢.

-----创建的XML -----

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 version="1.0" encoding="utf-8"?>
<DERMPAMappingTools_MapDetails>
  <XPos>684</XPos>
  <YPos>73</YPos>
</DERMPAMappingTools_MapDetails>>




-----我的代码-----




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

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

推荐答案

我的猜测是您需要重新创建文件,而不是打开或创建文件.您可能会覆盖文件中的现有数据.如果xml文件的先前内容是这样的(两个数字都是3个字符长):
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 version="1.0" encoding="utf-8"?>
<DERMPAMappingTools_MapDetails>
  <XPos>684</XPos>
  <YPos>731</YPos>
</DERMPAMappingTools_MapDetails>



新内容不会完全覆盖完整的xml,只剩下一个额外的>作为最后一个额外的字符.

祝你好运!



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

Good luck!


这篇关于XML错误随机发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆