我有一个简单的类,我试图从XML文档填充。
XML文件在根节点中有一个名为TrackingID的属性,我希望将其作为属性获取。出于某种原因,当我对类进行解除分类时,TrackingID为null。其他一切都很好。我已经在TrackingID属性上尝试了各种属性而没有运气。
有什么想法吗?
代码:
Dim faultXML As String = "<?xml version='1.0' encoding='UTF-8' ?>"
faultXML += "<myxmlns:Fault xmlns:myxmlns='http://somename/space' xmlns:myxmlns_base=http://somename/base' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://somename/space
faultXML += "myxmlns_102809.xsd http://idealliance.org/maildat/Specs/md091/myxmlns70C/base myxmlns_base_102809.xsd' myxmlns:TrackingID='160217057912'>"
faultXML += "<myxmlns:FaultCode>500</myxmlns:FaultCode>"
faultXML += "<myxmlns:FaultDescription>Some Description.</myxmlns:FaultDescription>"
faultXML += "</myxmlns:Fault>"
Dim fault As WebServiceFault
Dim Serializer As New XmlSerializer(GetType(WebServiceFault))
Using sr As New System.IO.StringReader(faultXML)
fault = DirectCast(Serializer.Deserialize(sr), WebServiceFault)
End Using
类别:
Imports System.Xml.Serialization
<System.SerializableAttribute(), _
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="http://somename/space"), _
System.Xml.Serialization.XmlRootAttribute([ElementName]:="Fault", [Namespace]:="http://somename/space", IsNullable:=False)> _
Public Class WebServiceFault
Private faultCodeField As String
Private faultDescriptionField As String
Private trackingIDField As String
Public Property FaultCode() As String
Get
Return Me.faultCodeField
End Get
Set(ByVal value As String)
Me.faultCodeField = value
End Set
End Property
Public Property FaultDescription() As String
Get
Return Me.faultDescriptionField
End Get
Set(ByVal value As String)
Me.faultDescriptionField = value
End Set
End Property
Public Property TrackingID() As String
Get
Return Me.trackingIDField
End Get
Set(ByVal value As String)
Me.trackingIDField = value
End Set
End Property
End Class
答案 0 :(得分:1)
我能够找到答案。添加以下属性解决了我的问题。
<System.Xml.Serialization.XmlAttributeAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Qualified)> _
Public Property TrackingID() As String
Get
Return Me.trackingIDField
End Get
Set(ByVal value As String)
Me.trackingIDField = value
End Set
End Property