Deserializing List of objects yields empty list
I have a RESTful WCF service that I am trying to send a list of tasks to.
I found on other questions that you have to encapsulate the list and I've
done that and the serialization looks like it is going through fine.
The XML that comes from serializing the list looks like:
<?xml version="1.0" encoding="utf-8"?>
<MobileRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TaskRequests xmlns="MyNameSpace">
<MobileTask>
<TaskArgs xsi:type="GetUser">
<Password>test</Password>
<UserName>t</UserName>
</TaskArgs>
<TaskID>1</TaskID>
<TaskType>GetUser</TaskType>
</MobileTask>
<MobileTask>
<TaskArgs xsi:type="GetUser">
<Password>test</Password>
<UserName>t2</UserName>
</TaskArgs>
<TaskID>2</TaskID>
<TaskType>GetUser</TaskType>
</MobileTask>
</TaskRequests>
</MobileRequest>
So the MobileRequest class has one property which is a list of MobileTask
objects. The code for the MobileRequest class is pretty straight forward
and looks like:
Imports System.Xml.Serialization
<Serializable(), DataContract(Name:="MobileRequest",
[Namespace]:="MyNameSpace")> _
Public Class MobileRequest
<XmlArray()> <DataMember(Name:="TaskRequests")> _
Public TaskRequests As List(Of MobileTask) = New List(Of MobileTask)
Public Sub New()
Me.TaskRequests = New List(Of MobileTask)
End Sub
End Class
The MobileTask class looks like:
Imports System.Xml.Serialization
<Serializable(), DataContract(Name:="MobileTask",
[Namespace]:="MyNamespace"), _
KnownType(GetType(Obj.GetUser)), XmlInclude(GetType(Obj.GetUser))> _
Public Class MobileTask
Public Enum TypesOfTasks As Integer
Unknown = 0
GetUser = 1
End Enum
<DataMember(Name:="TaskID")> _
Public TaskID As Integer
<DataMember(Name:="TaskType")> _
Public TaskType As TypesOfTasks
<DataMember(Name:="TaskArgs")> _
Public TaskArgs As Object
End Class
The type of TaskArgs changes depending on the TaskType. I've included an
XMLInclude and KnownType tag for each possible type that TaskArgs could
be. (this may be overkill but I did it when something with the
serialization wasn't working and just haven't attempted to clean it up
yet)
The code I am using in my tester app to attempt to Deserialize the XML is:
Using logg As New System.IO.MemoryStream()
Using sw As New System.IO.StreamWriter(logg)
sw.Write(txtSource.Text)
sw.Flush()
If logg.Length > 0 Then
Using reader As New System.IO.StreamReader(logg)
logg.Position = 0
Dim ser As New
System.Xml.Serialization.XmlSerializer(GetType(SMS_VendorObj.MobileRequest))
Dim results = ser.Deserialize(logg)
End Using
End If
End Using
End Using
This is from a simple winform setup that lets me paste in the XML so
txtSource is just a textbox that I put the XML I get from the
serialization into.
While running the test app I can step through the above deserialization
code and after the Dim results = ser.Deserialize(logg) line results is the
right type but the MobileTask list does not contain any items.
Am I missing something in the class files or is there something wrong with
the way I am trying to deserialize the list?
If I've missed important code please let me know and I will update with
whatever else is needed. I also edited out redundant pieces so I may have
cut too much without realizing it.
No comments:
Post a Comment