Search code examples
c#structxml-rpc

How to receive XMLRPC data structs with dynamic names in C#


I'm implementing a opensubtitles.com XMLRPC interface in C#.

One of the methods returns data this way:

[data] => Array
        (
            [46e33be00464c12e] => Array
                (
                    [MovieHash] => 46e33be00464c12e
                    [MovieImdbID] => 2816136
                    [MovieName] => "Game of Thrones" Two Swords
                    [MovieYear] => 2014
                    [MovieKind] => episode
                    [SeriesSeason] => 4
                    [SeriesEpisode] => 1
                    [SeenCount] => 19823
                    [SubCount] => 217
                )
)

As you can see the data array has an array inside it that dynamically takes the name of the hash returned, and returns values related to that hash inside.

This is another representation of it:

struct(
    struct( <-- list of movie info structures
      struct( <-- movie information structure (movieinfo)
        (string) [MovieHash],
        (string) [MovieImdbID],
        (string) [MovieName],
        (string) [MovieYear]
      ) [<video file hash>],
      struct( movieinfo ) [<video file hash>],

      ... more movie information structures go here (if any) ...

    ) [data],

and this is the XML representation:

     <member>
      <name>data</name>
      <value>
       <struct>
        <member>
         <name>d7aa0275cace4410</name>
         <value>
          <struct>
           <member>
            <name>MovieHash</name>
            <value><string>d7aa0275cace4410</string></value>
           </member>
           <member>
            <name>MovieImdbID</name>
            <value><string>371746</string></value>
           </member>
           <member>
            <name>MovieName</name>
            <value><string>Iron Man</string></value>
           </member>
           <member>
            <name>MovieYear</name>
            <value><string>2008</string></value>
           </member>
          </struct>

          ... more movie information structures go here (if any) ...

         </value>
        </member>
       </struct>
      </value>
     </member>

The question is how can receive such structs when I can't create a predefined struct to map values to it because the name is dynamic? The way it is now, I am mapping it to a struct named movieinfo that has all the fields inside, but it is returning null to everything. But I am sure the hashes exist on their servers because I used the hardcoded ones from their examples to make sure the test is working fine. Status of the call is "200 OK!"

The method's name is "CheckMovieHash". She Sorry I'm a bit new to XMLRPC.


Solution

  • You can use a dynamic type. If your xml fields aren't going to be changing you can still access the values using the . operator.

    Also, if you don't need the dynamic response, you can load the response and traverse the xml past the hash value and parse the children (the known data structure).