May*_*ron 5 xml vb.net deserialization
我有一个用于存储反序列化 XML 数据的类。我想让这个类向后兼容,以便它接受旧的根元素名称。
<XmlRoot(ElementName:="cancellation-response")>
Public Class ApplicantResponse
' properties go here!
End Class
Run Code Online (Sandbox Code Playgroud)
如果根元素是“applicant-response”或“cancellation-response” ,我希望解串器使用此类。
<XmlRoot(ElementName:="applicant-response")>
<XmlRoot(ElementName:="cancellation-response")>
Public Class ApplicantResponse
' properties go here!
End Class
Run Code Online (Sandbox Code Playgroud)
这可能吗?
当前 Visual Studio 抱怨使用上述方法:
属性“XmlRootAttribute”不能多次应用。
谢谢。
我的解决方案是将根名称动态传递到 XmlSerializer 中:
xmlRoot = New XmlRootAttribute(myRootName)
serializer = New XmlSerializer(GetType(ApplicantResponse), xmlRoot)
response = CType(serializer.DeSerialize(New StringReader(serializedResponse)), ApplicantResponse)
Run Code Online (Sandbox Code Playgroud)
其中 myRootName 是“申请人响应”或“取消响应”。