序列化XML问题(应用程序WPF) [英] Problem with Serialization XML (application WPF)

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

问题描述

我的序列化问题。我有一个包含其他对象列表的对象。我需要序列化这个对象,但总是会出错。主要对象称为DataGeneraleAudit。错误是:

反射类型期间发生错误



这是我的对象DataGeneraleAudit:



Hi,I have problem with my serialization. I have an object that contains a list of other objects. I need to serialize this object but always get an error. The principal Object is called "DataGeneraleAudit". The error is:
"An error occurred during the reflection type"

Here is my object 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"
    Public _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


End Class





作为y你可以在代码中看到,我从DataAudit列表中创建了一个对象。 Object DataAudit也适用于不同的方法和属性以及对象。



这是对象DataAudit:



As you can see in the code, I create an object from a list of DataAudit. The Object DataAudit is compose for differents methodes and property and objects too.

Here is the Object "DataAudit":

Public Class DataAudit 


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


#End Region

#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 ResultaAs As DataResultats
        Get
            Return _Resultats
        End Get

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

#End Region

#Region "Méthodes"
 

    Public Sub New()
   
    End Sub

#End Region

   
End Class





最后,这是序列化:





and finally, here is the serialization:

Public Class MainWindow
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
 Friend nomFichierConfigXml As String
    Friend Config As New DataGeneraleAudit

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

        Try

            ' Sérialization XML...
            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





请问哪里有问题?有人可以帮帮我吗?

谢谢



Where is the problem please? Somebody can help me?
Thank you

推荐答案

为什么要使用这种手动类序列化。帮自己一个大忙,切换到数据合约

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



这种方法可能是最好的方法。首先,它是非侵入式的:您不会将数据类修改为持久性,您不必实现任何接口或从任何所需的基类派生。您只需向参与合同所需的类型和成员添加一些属性,这些属性和成员无法在任何其他方面修改类型的行为。您可以简单地创建任何对象图,甚至可以使用循环引用(非树图)并将其存储为任何流,并在以后随时将其恢复。所以,这种方法是最容易使用的最好的方法。



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

< a href =http://www.codeproject.com/Answers/136502/How-can-I-utilize-XML-File-streamwriter-and-reader#answer1>我如何利用我的XML文件编写器和阅读器表单申请? [ ^ ],

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

解析json字符串数组 [ ^ ]。



-SA
Why would you use such a "manual" kind of serialization. Do yourself a great favor, switch to Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

This approach is probably the very best. First of all, it is non-intrusive: you don't modify your data classes to become persistent, you don't have to implement any interfaces or derive from any required base classes. You simply add some attributes to the types and members you need to participate in the contract, which cannot modify the behavior of your types in any other aspect. You can simply create any object graph, even having circular references (non-tree graph) and store it an any stream, and restore it in full any time later. So, this approach is the most robust yes the easiest to use.

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...[^],
deseralize a json string array[^].

—SA


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

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