Hello everyone, i have some issues with the deserialization of a .xml file in my project, im working with vb.net in vs2008.
This is my xml file:
<ArrayOfTest>
<Test>
<name></>
<author></>
<Questions>
<Question>
<number></>
<score></>
</Question>
<Question>
<number></>
<score></>
</Question>
<Question>
<number></>
<score></>
</Question>
</Questions>
</Test>
</ArrayOfTest>
<Test>
<name></>
<author></>
<Questions>
<Question>
<number></>
<score></>
</Question>
<Question>
<number></>
<score></>
</Question>
<Question>
<number></>
<score></>
</Question>
</Questions>
</Test>
</ArrayOfTest>
Hope it is understandable, my vb code is as follows:
public class test
'Code trimmed
public answers as new List(of answer)
End class
public class answer
'Code trimmed
End Class
Public Sub serializar(ByVal x As List(Of test))
Dim textWriter As TextWriter = New StreamWriter("test.xml")
serializer.Serialize(textWriter, x)
textWriter.Close()
End Sub
Public Function deserializar() As List(Of test)
Dim textReader As TextReader = New StreamReader("tests.xml")
tests = CType(serializer.Deserialize(textReader), List(Of test))
textReader.Close()
Return examenes
End Function
'Code trimmed
public answers as new List(of answer)
End class
public class answer
'Code trimmed
End Class
Public Sub serializar(ByVal x As List(Of test))
Dim textWriter As TextWriter = New StreamWriter("test.xml")
serializer.Serialize(textWriter, x)
textWriter.Close()
End Sub
Public Function deserializar() As List(Of test)
Dim textReader As TextReader = New StreamReader("tests.xml")
tests = CType(serializer.Deserialize(textReader), List(Of test))
textReader.Close()
Return examenes
End Function
Ok, that's all the relevant code I think, the problem is when i deserialize a xml file, the object i get from deserializing(the List(Of test)) contains the list of tests, but each test doesn't contain the list of answers. I dont know why it saves the xml file ok, but it doesnt load it back as it should.
Any help would be greatly appreciated.
And sorry for the vb code, i dont know c# very well.
Andrei D.
06/01/2011 - 04:46
In your xml file, there are Questions , but no answers.
That's why your desirialized xml will not be an object containing answers.
01/18/2012 - 15:09
Hi,
Answer class is not being deserialized because you are generating a new object. You must to implement custom serialization constructor to include this type. Change the member property for the List type with a public property.
Regards
CQ
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.