做了个可序列化的类,当xml反序列化的时候,如果哪个节点缺少了,那也不会报错,而是默认的将缺少的节点设为默认值。如何写才能在缺少节点的时候报错?
Public Shared Function Deserialize(ByVal xml As String, ByRef obj As DisbursementList, ByRef exception As System.Exception) As Boolean
exception = Nothing
obj = Nothing
Try
Dim stringReader As System.IO.StringReader = New System.IO.StringReader(xml)
Dim xmlTextReader As System.Xml.XmlTextReader = New System.Xml.XmlTextReader(stringReader)
Dim xmlSerializer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(DisbursementList))
obj = CType(xmlSerializer.Deserialize(xmlTextReader), DisbursementList)
Return True
Catch ex As System.Exception
exception = ex
Return False
End Try
End Function
把节点定义为必须的,也就是序列化时不能略过.