将 XML 数组反序列化为对象
Deserialize XML Array into Object
我正在尝试将 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
"EPP"class有什么问题吗?
您的 XML 有一个默认值 namespace of "urn:ietf:params:xml:ns:epp-1.0"
so you need to indicate that your classes are to be serialized into this namespace. The easiest way is to set <XmlRoot(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
for the root class, and <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.
我正在尝试将 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
"EPP"class有什么问题吗?
您的 XML 有一个默认值 namespace of "urn:ietf:params:xml:ns:epp-1.0"
so you need to indicate that your classes are to be serialized into this namespace. The easiest way is to set <XmlRoot(Namespace := "urn:ietf:params:xml:ns:epp-1.0")>
for the root class, and <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.