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

时间:2018-09-06 14:22:34

标签: xml class object xml-serialization stringwriter

我试图将一个类对象序列化为XML,但结果最终使其翻倍。我只将对象传递给函数一次,但是在对象通过序列化时就重复了。调试时,我注意到StringWriter的{​​{1}}字段设置为Length =2。我一直认为这是问题所在,但不知道如何进行更改。关于序列化,我还缺少其他东西吗?

我的XML序列化功能

CoreNewLine

我的班级对象

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

1 个答案:

答案 0 :(得分:0)

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