Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have this Xml that I have to deserialize.

HTML
<datatransfer>
    <start>
       <msgdate>YYYYMMDD</msgdate>
       <msgtime>HHMMSS</msgtime>
    </start>
    <details>
      <sailing>
        <departuredate>20100630</departuredate>
        <departuretime>100000</departuretime>
      </sailing>
      <sailing>
        <departuredate>20100710</departuredate>
        <departuretime>100000</departuretime>
      </sailing>
      <sailing>
        <departuredate>20100711</departuredate>
        <departuretime>100000</departuretime>
      </sailing>
    </details>
    <end>
      <nbr>1</nbr>
    </end>


Below are the classes with the get sets for the xml

VB
<XmlRoot(ElementName:="datatransfer")> _
   Public Class TtResponse
   <XmlElement("start")> _
   Public Property [responsestart] As responsestart
   <XmlElement("response")> _
   Public Property [TtResponseDetails] As List(Of TtResponseDetails)
   <XmlElement("end")> _
   Public Property [responseend] As responseend
   End Class

   <XmlRoot(ElementName:="details")> _
   Public Class TtResponseDetails : Inherits List(Of Ttsailing)
   Private _sailing As Ttsailing
   Public Property sailing As Ttsailing
   Get
       Return _sailing
   End Get
   Set(value As Ttsailing)
       _sailing = value
   End Set
   End Property
   End Class

   <XmlRoot(ElementName:="sailing")> _
   Public Class Ttsailing

   Private Property _departuredate As String
   Private Property _departuretime As String

   public Property departuredate As String
     Get
         Return _departuredate
     End Get
     Set(value As String)
         _departuredate = value
     End Set
   End Property

   Public Property departuretime As String
     Get
         Return _departuretime
     End Get
     Set(value As String)
         _departuretime = value
     End Set
   End Property

   End Class


I am deserializing using the code below and trying to display it.

VB
Dim Samples As New List(Of TtResponse)
       ' "c:\ftptest\Booking Confirmation"
       Dim Files As String() = Directory.GetFiles(Folder) ' File where the Booking confirmation is stored.
       For Each fl In Files


           'Deserialize XML file

           Dim objStreamReader As New StreamReader(fl)
           Dim i As New TtResponse
           Dim x As New XmlSerializer(i.GetType)
           i = x.Deserialize(objStreamReader)
           Samples.Add(i)
           objStreamReader.Close()
       Next

       Return Samples


However when I run it I get the error

Additional information: Unable to generate a temporary class (result=1).

error CS0030: Cannot convert type 'System.Collections.Generic.List <test.ttresponsedetails>'
to 'test.TtResponseDetails'

error CS0029: Cannot implicitly convert type 'test.TtResponseDetails'
to 'System.Collections.Generic.List<test.ttresponsedetails>'

error CS0030: Cannot convert type 'System.Collections.Generic.List<test.ttresponsedetails>'
to 'test.TtResponseDetails'

Can anyone help me out and tell me what I'm doing wrong?
Posted
Comments
Sergey Alexandrovich Kryukov 20-Aug-14 13:44pm    
Desesialize or parse? I mean, if you use serialization, you need to serialize yourself as well, to have consistent serialization/deserialization. Then it would not be your problem, the serializer would do it all. In other words, if the XML give, cannot be modified? If so, why?
—SA
Member 11025073 20-Aug-14 15:46pm    
I was wanting to deserialize as I receive the Xml so don't need to serialize it.
Sergey Alexandrovich Kryukov 20-Aug-14 16:02pm    
The question is not about "need", the question is about "can". Deserializing of something given is much more difficult then serializing + deserializing yourself by the same code.
—SA
Member 11025073 20-Aug-14 16:13pm    
Ok well what I will be getting is the XML above and the main thing I need to get off it is the sailings for the user to select which one they want. How would you recommend I do it? And if you have any code or tips it would be appreciated in fairly new to XML files and haven't used vb in years
Sergey Alexandrovich Kryukov 20-Aug-14 16:31pm    
I cannot get this part: what is "the sailings for the user"? Did you see my answer?
—SA

1 solution

Please see my comment to the question. If you have to work with the given file, serializers are poor helpers. Available serializers don't work with such a bare XML, they require a lot more metadata. So, more exactly, 1) you would need to create your own serializer matching this XML pattern, which would be really, really difficult or 2) you can simply parse this XML and populate some data class in type-specific code, member by member.

This is my short review of available parsers:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
Comments
Member 11025073 21-Aug-14 5:14am    
Thank you a parser is much handier to use.
Sergey Alexandrovich Kryukov 21-Aug-14 9:22am    
Serializer is always better, but only of the data is created by this serializer... :-)
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900