我试图将一个类对象序列化为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
答案 0 :(得分:0)
发现了问题。刚刚将对象字段从Public
更改为Private
。