views:

104

answers:

1

I have a simple class that I trying to populate from an XML document.

The XML file has an attribute called TrackingID in the root node which I would like to get as a property. For some reason, when I deseralize the class, the TrackingID is null. Everything else populates fine. I have tried various attributes on the TrackingID property with no luck.

Any ideas?

Code:

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

Class:

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
+1  A: 

I was able to find an answer. Adding the below attribute solved my problem.

 <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
Brett
Nicely done! I invalidated my test code by not qualifying all my elements and attributes with a namespace (the TrackingID attribute was the only thing with a namespace prefix).
Jeff Sternal