序列化XML问题 [英] Problem with Serialization XML

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

问题描述

问候,



简而言之,我正在制作一个WPF应用程序。该应用程序有5个存储数据的对象。这些对象被称为:



DataGeneraleAudit,DataAudit,DataTechnologie,dataacquisition,DataResultat



对象DataGeneraleAudit包含一个DataAudit对象列表。



对象DataAudit包含其他对象:DataTechnologie,dataacquisition和DataResultat。



我想要SERIALIZE DataGeneraleAudit对象来恢复所有其他对象的数据。



这里我留下DataGeneraleAudit:

  Imports  System.Runtime.Serialization 
Imports System.IO
Imports System.Xml.Serialization



< XmlRoot( DonneesLogiciel)> 公开 DataGeneraleAudit
公开 Sub ()

结束 Sub
#RegionDéclaration
朋友 _DataTotale 作为列表( DataAudit)
#End Region

#RegionPropiétés

< XmlAttribute( Total)> 公共 属性 DonneeTotale()作为列表( DataAudit)
获取
返回 _DataTotale
结束 获取

设置 ByVal As 列表( DataAudit))
_DataTotale = value
结束 设置
结束 物业

#End Region

#RegionMéthodes

公开 Sub ()
_DataTotale = 列表( DataAudit)
结束 Sub

公共 Sub AddDataTotale( ByVal AllDonnees 作为 字符串
Dim DatosTotal = DataAudit(AllDonnees)
.DonneeTotale.Add(DatosTotal)

结束 Sub
#End Region
结束



现在我向您展示DataAu对象dit:



 公共  DataAudit 

#Region声明

私有 _Technologie 作为 DataTechnologie
私有 _Acquisition 作为 DataAcquisition
私有 _Resultats 作为 DataResultats


私有 _textNomAudit As String
私有 _textDateAudit As String


#End Region


私人 _all Donnees As String

Sub ByVal AllDonnees As 字符串
' TODO:完整成员初始化
_allDonnees = AllDonnees
结束 Sub

#RegionPropiétés
公开 属性 Technologie()作为 DataTechnologie
获取
< span class =code-keyword>返回 _Technologie
结束 获取

设置 ByVal 正如 DataTechnologie)
_Technologie = value
结束 设置
结束 属性


公共 属性 Acquisition()作为 DataAcquisition
获取
返回 _Acquisition
结束 获取

设置 ByVal As DataAcquisition)
_Acquisition = value
结束 设置
结束 属性

公共 属性结果正如 DataResultats
获取
返回 _Resultats
< span class =code-keyword>结束
获取

设置 ByVal value As DataResultats)
_Resultats = value
结束 设置
结束 属性

#End Region

#RegionPropiétés
公开 属性 _NomAudit()正如 字符串
获取
返回 _textNomAudit
结束 获取

设置 ByVal 字符串
_textNomAudit = value
结束 设置
结束 物业


公共 属性 _DateAudit()作为 < span class =code-keyword> String
获取
返回 _textDateAudit
结束 获取

设置(< span class =code-keyword> ByVal value As String
_textDateAudit = value
结束 设置
结束 属性


#End Region
#RegionMéthodes

' Creation de constructeur
公共 Sub ()

结束 Sub




公共 Sub AllDonneesAudit( ByVal technologie As DataTechnologie, ByVal acquisition As DataAcquisition, ByVal 结果作为 DataResultats,_
ByVal TextoNomAudit 作为 字符串 ByVal TextoDateAudit 作为 字符串 ByVal TextoNuOccupants 作为 字符串

Me .Technologie = technologie
Me .Acquisition = acquisition
.Resultats = resultats
._ NomAudit = TextoNomAudit
._ DateAudit = TextoDateAudit
结束 Sub

#End Region

结束 Class





其他对象只有几个属性。



最后,我进行序列化的类(MainWindow类):



 朋友配置作为  DataGeneraleAudit()
私人 < span class =code-keyword> Sub MainWindow_Closing( ByVal sender As System。< span class =code-keyword> Object , ByVal e As System.ComponentModel。 CancelEventArgs)句柄 MyBase .Closing

尝试

Dim xSerializer As XmlSerializer = XmlSerializer( GetType (DataGeneraleAudit))
Dim writer As TextWriter = StreamWriter( nomFichierConfigXml
xSerializer.Serialize(writer) ,Config)
writer.Close()

Catch ex As 例外
MsgBox(ex.Message)
结束 尝试

结束 Sub



现在当我尝试序列化DataGeneraleAudit时出现问题我总是收到以下错误:



反射类型期间发生错误



据我所知,这种类型的错误来自序列化。但我无法解决它。



有人可以帮我解决这个小问题吗?



谢谢

解决方案

为什么要使用这种序列化?我会说,它已经过时了,而且设计得不是很好。相反,使用数据合同

http:/ /msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



这种方法最容易使用,非侵入性和最全面。您不必以任何方式修改数据类型:无需实现任何接口,派生自任何特定类或类似的类。您只需使用适当的属性标记某些类型和成员(您希望成为合同的一部分)。您可以存储并稍后恢复任何对象图,即使其中包含循环引用。



请参阅我过去的答案,我提倡这种方法:

如何我可以在表单应用程序中使用XML文件写入器和阅读器吗? [ ^ ],

创建属性文件...... [ ^ ]。



-SA

Regards,

In short, i'm making a WPF application. The application has 5 objects that store data. The objects are called:

DataGeneraleAudit, DataAudit, DataTechnologie, dataacquisition, DataResultat

The object DataGeneraleAudit contains a list of DataAudit object.

The object DataAudit contains the others objects: DataTechnologie, dataacquisition and DataResultat.

I want to SERIALIZE DataGeneraleAudit object for recover data from all others objects.

Here I leave the DataGeneraleAudit:

Imports System.Runtime.Serialization
Imports System.IO
Imports System.Xml.Serialization



<XmlRoot("DonneesLogiciel")> Public Class DataGeneraleAudit
    Public Sub New()

    End Sub
#Region "Déclaration"
    Friend _DataTotale As List(Of DataAudit)
#End Region

#Region "Propiétés"
 
    <XmlAttribute("Total")> Public Property DonneeTotale() As List(Of DataAudit)
        Get
            Return _DataTotale
        End Get

        Set(ByVal value As List(Of DataAudit))
            _DataTotale = value
        End Set
    End Property

#End Region

#Region "Méthodes"
 
      Public Sub New()
     _DataTotale = New List(Of DataAudit)
    End Sub

    Public Sub AddDataTotale(ByVal AllDonnees As String) 
        Dim DatosTotal = New DataAudit(AllDonnees)
        Me.DonneeTotale.Add(DatosTotal)

    End Sub
#End Region
End Class


Now I show you the object DataAudit:

Public Class DataAudit 

#Region "Declarations"
 
    Private _Technologie As DataTechnologie
    Private _Acquisition As DataAcquisition
    Private _Resultats As DataResultats


    Private _textNomAudit As String
    Private _textDateAudit As String

    
#End Region


    Private _allDonnees As String

    Sub New(ByVal AllDonnees As String)
        'TODO: Complete member initialization 
                _allDonnees = AllDonnees
    End Sub

#Region "Propiétés"
    Public Property Technologie() As DataTechnologie
        Get
            Return _Technologie
        End Get

        Set(ByVal value As DataTechnologie)
            _Technologie = value
        End Set
    End Property


    Public Property Acquisition() As DataAcquisition
        Get
            Return _Acquisition
        End Get

        Set(ByVal value As DataAcquisition)
            _Acquisition = value
        End Set
    End Property

    Public Property Resultats As DataResultats
        Get
            Return _Resultats
        End Get

        Set(ByVal value As DataResultats)
            _Resultats = value
        End Set
    End Property

#End Region

#Region "Propiétés "
        Public Property _NomAudit() As String
        Get
            Return _textNomAudit
        End Get

        Set(ByVal value As String)
            _textNomAudit = value
        End Set
    End Property

    
    Public Property _DateAudit() As String
        Get
            Return _textDateAudit
        End Get

        Set(ByVal value As String)
            _textDateAudit = value
        End Set
    End Property

           
#End Region
#Region "Méthodes"
 
    'Creation de constructeur
    Public Sub New()

    End Sub




    Public Sub AllDonneesAudit(ByVal technologie As DataTechnologie, ByVal acquisition As DataAcquisition, ByVal resultats As DataResultats, _
                               ByVal TextoNomAudit As String, ByVal TextoDateAudit As String, ByVal TextoNuOccupants As String)

        Me.Technologie = technologie
        Me.Acquisition = acquisition
        Me.Resultats = resultats
        Me._NomAudit = TextoNomAudit
        Me._DateAudit = TextoDateAudit
          End Sub

#End Region

End Class



The other objects have only a few attributes.

Finally, the class where i made the serialization (MainWindow class):

Friend Config As New DataGeneraleAudit()
Private Sub MainWindow_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        Try
        
            Dim xSerializer As XmlSerializer = New XmlSerializer(GetType(DataGeneraleAudit))
            Dim writer As TextWriter = New StreamWriter("nomFichierConfigXml")
            xSerializer.Serialize(writer, Config)
            writer.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


Now the problem arises when I try to serialize a DataGeneraleAudit I always get the following error:

"An error occurred During The reflection type"

As far as I found, this type of error comes from the serialization. But I could not fix it.

Could someone help me with this little problem?

thanks

解决方案

Why using this kind of serialization at all? I would say, it's obsolete and it wasn't very well designed. Instead, use Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is the easiest to use, non-intrusive and most comprehensive. You don't have to modify your data types in any way: no need to implement any interfaces, derive from any certain classes or anything like that. You only mark some types and members (those you want to be part of contract) with appropriate attributes. You can store and later restore any object graph, even with circular references in it.

Please see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA


这篇关于序列化XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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