Results 1 to 3 of 3

Thread: [RESOLVED] XML Deserialization (Hydration)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Resolved [RESOLVED] XML Deserialization (Hydration)

    Hi!
    I'm having a problem that's driving me crazy; I can't understand how to convert the XML structure into a class structure (that I want to use to hydrate a XML document).

    The XML document looks like this:
    xml Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <artists xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.spotify.com/ns/music/1">
    3.   <opensearch:Query role="request" startPage="1" searchTerms="Piano Tribute Players"/>
    4.   <opensearch:totalResults>2</opensearch:totalResults>
    5.   <opensearch:startIndex>0</opensearch:startIndex>
    6.   <opensearch:itemsPerPage>100</opensearch:itemsPerPage>
    7.   <artist href="spotify:artist:4Xx6QMLTWppMwdABkN0Afj">
    8.     <name>Piano Tribute Players</name>
    9.  
    10.     <popularity>0.73531</popularity>
    11.   </artist>
    12.   <artist href="spotify:artist:4oG2lcwDOqbB74BOE4O7o7">
    13.     <name>Piano Players Tribute</name>
    14.     <popularity>0.50211</popularity>
    15.   </artist>
    16. </artists>

    And I need to get all that information into a class but I'm really lost.
    To begin with, I don't know how to create the sub-node (artist) in a class, I've tried with Public Structure and Public Property but I think that it needs to be another class, am I right?

    Here's what I currently have:
    vb.net Code:
    1. <Serializable(), XmlRoot()> _
    2. Public Class artists
    3.     Public totalResults As Integer
    4.     Public startIndex As Integer
    5.     Public itemsPerPage As Integer
    6.     Public Class artist
    7.         Public name As String
    8.         Public popularity As Double
    9.     End Class
    10. End Class

    Another thought that I have is that the artist node isn't only one, so how would I do to create multiple artist classes....

    Please help me!
    //Zeelia
    Last edited by Zeelia; Apr 25th, 2010 at 05:46 PM. Reason: Resolved

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: XML Deserialization (Hydration) from URI

    try this. i used this:

    http://www.bonesoft.com/XmlModeler/Demo.aspx

    it deserializes your xml example ok...

    vb Code:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Xml.Serialization
    4.  
    5. ' <summary>Represents an xml root artists document element.</summary>
    6. <XmlRoot("artists", Namespace:="http://www.spotify.com/ns/music/1")> _
    7. Public Class Artists
    8.     Private p_attribute_xmlns As XmlSerializerNamespaces
    9.     Private p_element_query As Query
    10.     Private p_element_totalresults As String
    11.     Private p_element_startindex As String
    12.     Private p_element_itemsperpage As String
    13.     Private p_element_artist As Artist()
    14.  
    15.     ' <summary>XmlSerializerNamespaces attribute.</summary>
    16.     <XmlNamespaceDeclarations()> _
    17.     Public Property xmlns() As XmlSerializerNamespaces
    18.         Get
    19.             Return Me.p_attribute_xmlns
    20.         End Get
    21.         Set(ByVal Value As XmlSerializerNamespaces)
    22.             Me.p_attribute_xmlns = Value
    23.         End Set
    24.     End Property
    25.  
    26.     ' <summary>Query xml element.</summary>
    27.     <XmlElement("Query", Namespace:="http://a9.com/-/spec/opensearch/1.1/")> _
    28.     Public Property Query() As Query
    29.         Get
    30.             Return Me.p_element_query
    31.         End Get
    32.         Set(ByVal Value As Query)
    33.             Me.p_element_query = Value
    34.         End Set
    35.     End Property
    36.  
    37.     ' <summary>String totalResults element.</summary>
    38.     <XmlElement("totalResults", Namespace:="http://a9.com/-/spec/opensearch/1.1/")> _
    39.     Public Property TotalResults() As String
    40.         Get
    41.             Return Me.p_element_totalresults
    42.         End Get
    43.         Set(ByVal Value As String)
    44.             Me.p_element_totalresults = Value
    45.         End Set
    46.     End Property
    47.  
    48.     ' <summary>String startIndex element.</summary>
    49.     <XmlElement("startIndex", Namespace:="http://a9.com/-/spec/opensearch/1.1/")> _
    50.     Public Property StartIndex() As String
    51.         Get
    52.             Return Me.p_element_startindex
    53.         End Get
    54.         Set(ByVal Value As String)
    55.             Me.p_element_startindex = Value
    56.         End Set
    57.     End Property
    58.  
    59.     ' <summary>String itemsPerPage element.</summary>
    60.     <XmlElement("itemsPerPage", Namespace:="http://a9.com/-/spec/opensearch/1.1/")> _
    61.     Public Property ItemsPerPage() As String
    62.         Get
    63.             Return Me.p_element_itemsperpage
    64.         End Get
    65.         Set(ByVal Value As String)
    66.             Me.p_element_itemsperpage = Value
    67.         End Set
    68.     End Property
    69.  
    70.     ' <summary>Artist[] artist xml element.</summary>
    71.     <XmlElement("artist", Namespace:="")> _
    72.     Public Property Artists() As Artist()
    73.         Get
    74.             Return Me.p_element_artist
    75.         End Get
    76.         Set(ByVal Value As Artist())
    77.             Me.p_element_artist = Value
    78.         End Set
    79.     End Property
    80. End Class
    81.  
    82. ' <summary>Represents a artists.Query node.</summary>
    83. Public Class Query
    84.     Private p_attribute_role As String
    85.     Private p_attribute_startpage As Int32
    86.     Private p_attribute_searchterms As String
    87.  
    88.     ' <summary>String role attribute.</summary>
    89.     <XmlAttribute("role")> _
    90.     Public Property Role() As String
    91.         Get
    92.             Return Me.p_attribute_role
    93.         End Get
    94.         Set(ByVal Value As String)
    95.             Me.p_attribute_role = Value
    96.         End Set
    97.     End Property
    98.  
    99.     ' <summary>Int32 startPage attribute.</summary>
    100.     <XmlAttribute("startPage")> _
    101.     Public Property StartPage() As Int32
    102.         Get
    103.             Return Me.p_attribute_startpage
    104.         End Get
    105.         Set(ByVal Value As Int32)
    106.             Me.p_attribute_startpage = Value
    107.         End Set
    108.     End Property
    109.  
    110.     ' <summary>String searchTerms attribute.</summary>
    111.     <XmlAttribute("searchTerms")> _
    112.     Public Property SearchTerms() As String
    113.         Get
    114.             Return Me.p_attribute_searchterms
    115.         End Get
    116.         Set(ByVal Value As String)
    117.             Me.p_attribute_searchterms = Value
    118.         End Set
    119.     End Property
    120. End Class
    121.  
    122. ' <summary>Represents a artists.artist node.</summary>
    123. Public Class Artist
    124.     Private p_attribute_href As String
    125.     Private p_element_name As String
    126.     Private p_element_popularity As String
    127.  
    128.     ' <summary>String href attribute.</summary>
    129.     <XmlAttribute("href")> _
    130.     Public Property Href() As String
    131.         Get
    132.             Return Me.p_attribute_href
    133.         End Get
    134.         Set(ByVal Value As String)
    135.             Me.p_attribute_href = Value
    136.         End Set
    137.     End Property
    138.  
    139.     ' <summary>String name element.</summary>
    140.     <XmlElement("name")> _
    141.     Public Property Name() As String
    142.         Get
    143.             Return Me.p_element_name
    144.         End Get
    145.         Set(ByVal Value As String)
    146.             Me.p_element_name = Value
    147.         End Set
    148.     End Property
    149.  
    150.     ' <summary>String popularity element.</summary>
    151.     <XmlElement("popularity")> _
    152.     Public Property Popularity() As String
    153.         Get
    154.             Return Me.p_element_popularity
    155.         End Get
    156.         Set(ByVal Value As String)
    157.             Me.p_element_popularity = Value
    158.         End Set
    159.     End Property
    160. End Class

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    519

    Re: XML Deserialization (Hydration) from URI

    Thanks a lot paul, I think I understand how deserialization works now.

    Cheers,
    //Zeelia

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width