在类对象的XML序列化期间发生重复

问题描述 投票:0回答:1

我正在尝试将类对象序列化为XML,但结果会使其加倍。我只将对象传递给函数一次,但在通过序列化时重复。调试时,我注意到我的StringWriterCoreNewLine字段设置为Length = 2.我一直认为这是问题,但不知道如何改变它。关于我的序列化,我还缺少其他的东西吗?

我的XML序列化功能

Public Shared Function ToXml(ByVal obj As Object) As String
    Try
        Dim serializer As XmlSerializer = New XmlSerializer(obj.[GetType]())

        Dim stringWrit As StringWriter = New StringWriter()
        Using writer = XmlWriter.Create(stringWrit)
            serializer.Serialize(stringWrit, obj)
            Return stringWrit.ToString()
        End Using

    Catch ex As Exception

        DumpException(ex)
        Return ex.ToString()
    End Try

End Function

我的类对象

<Serializable>
Public Class SPFolderOver

Public _files As List(Of SPFile)

Public _folders As List(Of SPFolderOver)

Public _name As String

Public _fullPath As String

<NonSerialized>
<XmlIgnore>
Public _props As Dictionary(Of EmailProperty, String)

Public _sp As Sharepoint

Public Property SPFiles() As List(Of SPFile)
    Get
        Return _files
    End Get
    Set(ByVal value As List(Of SPFile))
        _files = value
    End Set
End Property

Public Property SPFolders() As List(Of SPFolderOver)
    Get
        Return _folders
    End Get
    Set(ByVal value As List(Of SPFolderOver))
        _folders = value
    End Set
End Property


Public Property FolderName() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property

<XmlIgnore>
Public Property Properties() As Dictionary(Of EmailProperty, String)
    Get
        Return _props
    End Get
    Set(ByVal value As Dictionary(Of EmailProperty, String))
        _props = value
    End Set
End Property

Public Property Folder() As Object
    Get
        'Return _folderObj
        Return Nothing
    End Get
    Set(ByVal value As Object)
        '_folderObj = value
    End Set
End Property


Public Property FullPath() As String
    Get
        Return _fullPath
    End Get
    Set(ByVal value As String)
        _fullPath = value
    End Set
End Property


Public Property SP() As Sharepoint
    Get
        Return _sp
    End Get
    Set(ByVal value As Sharepoint)
        _sp = value
    End Set
End Property
xml class object xml-serialization stringwriter
1个回答
0
投票

发现了这个问题。刚刚将对象字段从Public更改为Private

© www.soinside.com 2019 - 2024. All rights reserved.