VB.net Deserializer and namepaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reaperfighter
    New Member
    • Jun 2012
    • 1

    VB.net Deserializer and namepaces

    Hi all,
    I have some XML that I am trying to deserialize using visual Basic 2010, I did not serialize the XML, I just want to deserialize it.

    The issue I am having is that the element has a namespace of :q2 and a type of q2: and I am having a challenge in trying to reflect that in my code. An example of the XML is as follows:

    Code:
    <Record xmlns:q2="http://www.happy.com" xsi:type="q2:boats">
            <q2:boats>
              <q2:Command>call the captain</q2:Command>
              <q2:Parameters>Microphone</q2:Parameters>
              <q2:Type>Loud</q2:Type>
              <q2:Order>117</q2:Order>
            </q2:boats>
    The class I created to store my data is as follows:

    Code:
    Partial Public Class Boats
            Private mstrCommand As String
            Private mstrParameters As String
            Private mstrType As String
            Private mstrOrder As String
    
            Public Sub New()
            End Sub
    
            Public Sub New(ByVal Command As String, _
                           ByVal Parameters As String, _
                           ByVal Type As String, _
                           ByVal Order As String)
    
                Command = Command
                Parameters = mstrParameters
                Type = mstrType
                Order = mstrOrder
            End Sub
    
            <XmlElement("q2:Command")> _
            Public Property Command() As String
                Get
                    Return mstrCommand
                End Get
                Set(ByVal Value As String)
                    mstrCommand = Value
                End Set
            End Property
            <XmlElement("q2:Parameters")> _
            Public Property Parameters() As String
                Get
                    Return mstrParameters
                End Get
                Set(ByVal Value As String)
                    mstrParameters = Value
                End Set
            End Property
    
            <XmlElement("q2:Type")> _
            Public Property Type() As String
                Get
                    Return mstrType
                End Get
                Set(ByVal Value As String)
                    mstrType = Value
                End Set
            End Property
            <XmlElement("q2:Order")> _
            Public Property Order() As String
                Get
                    Return mstrOrder
                End Get
                Set(ByVal Value As String)
                    mstrOrder = Value
                End Set
            End Property
        End Class
    This will NOT work because there is no element called q2:command for example, so I have deserialization issues. How can I change my class to reflect the XML correctly?

    I have seen some examples where people suggest that adding the namespace into a serializer will generate the q1: but there is nothing about how to deserialize it, especially when you did not serialize the XML in the first place :)

    Any help would be much appreciated.
    Last edited by PsychoCoder; Jun 14 '12, 07:00 PM. Reason: Code tags added
Working...