我尝试将XML字符串反序列化为对象,但它给了我以下错误:
System.InvalidOperationException:不期望。
XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp
xmlns="urn:ietf:params:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<response>
<result code="1000">
<msg>Command completed successfully</msg>
</result>
<trID>
<svTRID>20151019181132-1C16AD22-396-0001</svTRID>
</trID>
</response>
</epp>
CLASS:
Imports System.Xml.Serialization
<Serializable(), XmlRoot("epp")>
Public Class EPP
Public Sub New()
Response = New Response()
End Sub
<XmlElement(ElementName:="response")> _
Public Property Response As Response
End Class
<Serializable()>
Public Class Response
Public Sub New()
result = New result()
trID = New trID()
End Sub
<XmlElement(ElementName:="result")> _
Public Property result As result
<XmlElement(ElementName:="trID")> _
Public Property trID As trID
End Class
<Serializable()>
Public Class trID
Public Sub New()
svTRID = String.Empty
End Sub
<XmlElement(ElementName:="svTRID")> _
Public Property svTRID As String
End Class
<Serializable()>
Public Class result
Public Sub New()
msg = String.Empty
End Sub
<XmlElement(ElementName:="msg")> _
Public Property msg As String
End Class
以下是代码:
Dim r As New EPP
Dim x As New XmlSerializer(r.GetType)
Using s As New StringReader(response.Xml)
r = x.Deserialize(s)
End Using
&#34; EPP&#34;是否有任何问题?类?
答案 0 :(得分:0)
您的XML默认namespace为"urn:ietf:params:xml:ns:epp-1.0"
,因此您需要指明您的类将被序列化到此命名空间中。最简单的方法是为根类设置<XmlRoot(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
,为余数设置<XmlType(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
:
<XmlRoot("epp", Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
Public Class EPP
Public Sub New()
Response = New Response()
End Sub
<XmlElement(ElementName:="response")> _
Public Property Response As Response
End Class
<XmlType(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
Public Class Response
Public Sub New()
result = New result()
trID = New trID()
End Sub
<XmlElement(ElementName:="result")> _
Public Property result As result
<XmlElement(ElementName:="trID")> _
Public Property trID As trID
End Class
<XmlType(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
Public Class trID
Public Sub New()
svTRID = String.Empty
End Sub
<XmlElement(ElementName:="svTRID")> _
Public Property svTRID As String
End Class
<XmlType(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
Public Class result
Public Sub New()
msg = String.Empty
End Sub
<XmlElement(ElementName:="msg")> _
Public Property msg As String
End Class
原型fiddle。