locked
XML Classes (Paste As) RRS feed

  • Question

  • User-284642143 posted

    I have some XML as below

    <customer id="2345">
        <reference>ABCDEF</reference>
        <querydate>2017-12-10T15:00:14.044Z</querydate>
        <contact>
          <telnumber>1234567890</telnumber>
        </contact>
        <link rel="web" href="http://www.customer.com" />
    </customer>

    I usually create a class to read the data and then hold the values within the class properties, in this case a snippet of what i would have had

    Public Class Customer
        Public Property Reference as string
        Public Property Querydate as Datetime
        ....
        .....
    End Class

    This time round, i read an article which shows the option Paste as XML within VS (under Edit menu). Using this feature i have the below result

    '''<remarks/>
    <System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _
     System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
    Partial Public Class customer
    
        Private referenceField As String
    
        Private querydateField As Date
    
        Private contactField As customerContact
    
        Private linkField As customerLink
    
        Private idField As UShort
    
        '''<remarks/>
        Public Property reference() As String
            Get
                Return Me.referenceField
            End Get
            Set(value As String)
                Me.referenceField = Value
            End Set
        End Property
    
        '''<remarks/>
        Public Property querydate() As Date
            Get
                Return Me.querydateField
            End Get
            Set(value As Date)
                Me.querydateField = Value
            End Set
        End Property
    
        '''<remarks/>
        Public Property contact() As customerContact
            Get
                Return Me.contactField
            End Get
            Set(value As customerContact)
                Me.contactField = Value
            End Set
        End Property
    
        '''<remarks/>
        Public Property link() As customerLink
            Get
                Return Me.linkField
            End Get
            Set(value As customerLink)
                Me.linkField = Value
            End Set
        End Property
    
        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute()> _
        Public Property id() As UShort
            Get
                Return Me.idField
            End Get
            Set(value As UShort)
                Me.idField = Value
            End Set
        End Property
    End Class
    
    '''<remarks/>
    <System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
    Partial Public Class customerContact
    
        Private telnumberField As UInteger
    
        '''<remarks/>
        Public Property telnumber() As UInteger
            Get
                Return Me.telnumberField
            End Get
            Set(value As UInteger)
                Me.telnumberField = Value
            End Set
        End Property
    End Class
    
    '''<remarks/>
    <System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _
    Partial Public Class customerLink
    
        Private relField As String
    
        Private hrefField As String
    
        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute()> _
        Public Property rel() As String
            Get
                Return Me.relField
            End Get
            Set(value As String)
                Me.relField = Value
            End Set
        End Property
    
        '''<remarks/>
        <System.Xml.Serialization.XmlAttributeAttribute()> _
        Public Property href() As String
            Get
                Return Me.hrefField
            End Get
            Set(value As String)
                Me.hrefField = Value
            End Set
        End Property
    End Class

    This has me confused, its created 3 classes (and that being partial) where i was expecting something like what i presented above. So is this way of creating classes for a different purpose? If yes what could i google to learn whats these classes doing or have i been doing something wrong with my approach to hold and pass XML data around?

    Friday, January 26, 2018 3:53 PM

Answers

  • User475983607 posted

    EssCee

    This has me confused, its created 3 classes (and that being partial) where i was expecting something like what i presented above.

    Basically, it takes multiple classes to describe the XML. 

    EssCee

    So is this way of creating classes for a different purpose? If yes what could i google to learn whats these classes doing or have i been doing something wrong with my approach to hold and pass XML data around?

    This question is related to creating an object model.  There is nothing that you can Google that will explain your one-off example above.  Your best option is to compare the object to the XML or try populating the generated customer class. I think populating the customer class will help with your understanding.  Also look up the xml attributes in the .NET docs soyou understand what they are doing.

    You can also read the .NET docs related to XML serialization.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/introducing-xml-serialization

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/examples-of-xml-serialization

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 26, 2018 4:21 PM