你好,游客 登录 注册 搜索
背景:
阅读新闻

序列化 根级别上的数据无效。 行 1,位置 1 -

[日期:2013-03-19] 来源:  作者: [字体: ]

最近在做反序列化的时候遇到过     根级别上的数据无效。 行 1,位置 1

最后发现是在序列化的时候代码不标准所致

序列化代码:

    ''' <summary>
    ''' 将Pack文件保存到本地文件,并返回文件名
    ''' </summary>
    ''' <param name="pk">List(Of PackageData)</param>
    ''' <param name="fileName">fileName</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function SavePackToFile(ByVal pk As List(Of PackageData), Optional ByVal fileName As String = "") As String
        Try
            If fileName.Trim.Length <= 0 Then fileName = Guid.NewGuid().ToString + ".pak"
            Dim destFile As String = AppDomain.CurrentDomain.BaseDirectory + "\Download\" + fileName

            Dim ser As New DataContractSerializer(GetType(List(Of PackageData)))
            Dim wx As New Xml.XmlTextWriter(destFile, System.Text.Encoding.UTF8)
            ser.WriteObject(wx, pk)
            wx.Close()
        Catch ex As Exception
            Throw ex
        End Try
        Return fileName
    End Function

反序列化:

    ''' <summary>
    ''' 反序列化PackageData
    ''' </summary>
    ''' <param name="s"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Deserialize(ByVal s As String) As List(Of PackageData)
        Try

            Dim txter As TextReader = New StringReader(s.Trim())
            Dim reader As New XmlTextReader(txter)

            Dim ser As New DataContractSerializer(GetType(List(Of PackageData)))
            Dim datas As List(Of PackageData) = CType(ser.ReadObject(reader), List(Of PackageData))
            reader.Close()
            txter.Close()
            Return datas
        Catch ex As Exception
            Throw ex
        End Try
        Return Nothing
    End Function

这样之后不会再报错了。






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻