Web services and associative arrays

Hello,
We're creating a web service that allows retreieval of NFL player statistics.  We were going to use PHP, but now we are going to use C# since setting up the web service seems to be easier.  I originally planned to return the result set in a associative array in PHP.  For example for http://www.nfl.com/players/playerpage/492974 I would have something like:
array
(
  "first_name" => "Julius",
  "last_name"  => "Jones",
  "weight" => "205",
   ...
   "rushing" => array("stats" => array("Team", "G","GS", "Att", "Yards", "Avg", "Lg", "TD", "20+", "FD"),
                                     "2004" => array(....)
                                    )
   ...
)

What would be the best way to implement this in C#?  My main goal is an easy way to provice access to a result set.  Is it possible to return a class in such a manner?

Thanks,
Yasir
LVL 3
Yasir_MalikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CrazyIvan007Commented:
The best way to do this is to create a new class of CollectionBase


public class PlayerSets : CollectionBase
{
      public void Add(PlayerSet _ps)
      {
            this.List.Add(_ps);
      }

      public void Remove(PlayerSet _ps)
      {
            this.List.Remove(_ps);
      }

      public PlayerSet Find(string StringToFind)
      {
            PlayerSet _returnSet = null;
            
            foreach (PlayerSet _ps in this.List)
            {
                  // Include your own Find Createria if you want
            }

            return (_returnset);
      }
}

public class PlayerSet
{
      private string _FirstName = null;
      private string _LastName = null;

      ...

      public string FirstName
      {
            get { return _FirstName; }
            set { _FirstName = value; }
      }

      public string LastName
      {
            get { return _LastName; }
            set { _LastName = value; }
      }
}

First you create a CollectionBase with Add/Remove/Find, then you create a new class of your Datasets. After that you can simply call up your Dataset with

PlayerSets _playersets = new PlayerSets();

To Add new items to them

PlayerSet _player = new PlayerSet();
_player.FirstName = "First Name";
_player.LastName = "Last Name";
_playersets.Add(_player);

You can cygle through the sets

foreach (PlayerSet _player in _playersets)
{ }

and implement new Features and Find Algorithms. I think that would be the best for your needs.
0
Yasir_MalikAuthor Commented:
It looks like you can't return Hashtables.  I'm not that familiar yet with web services, but will another application program written in, say, PHP be able to deal with what you have?
0
CrazyIvan007Commented:
You can return Hashtables.. you can use a Hashtable Set

Hashtable hTable = new Hashtable();

hTable["key"] = value;

or

PlayerSet _player = new PlayerSet();
// Set the _player Vars

hTable["key"] = _player;


and later derefernce the object to your Set

PlayerSet _player = (PlayerSet)hTable["key"];


But using CollectionBase is more accurate :)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

NipNFriar_TuckCommented:
If you do what CrazyIvan007 said but use the DictionaryBase you in essence have a strongly typed dictionary.
0
edj371Commented:
It seems like you have some hierarchical data that you need to pass around, and it sounds like you may still be interested in PHP.

Do you want to keep from tying yourself down to return values and data types specific to a language?  Have you thought about xml, and considered that it might be the right tool for the job?

It not only crosses languages (really good XML parsers abound in nearly all languages), but it will allow you to create data flow that is independent of language, since the interfaces are in xml.  You can query xml and return values as with a dictionary or collections of values.  Really, for hierarchical data that you'll be passing around, xml is the way to go.  In fact, DataSets in C# are merely xml with nice wrapper classes.  Another nice feature of xml is, given an xml node, like the I've included below, it would be very easy, if desired, to slap an xsl transform on it and turn it into some good looking html.  Then all you have to do is modify the xsl transform and get different html, keeping your xml structure the same.

I have included the xml node below as an initial, and very rough, pass at encapsulating your data.  The structure of your xml will depend on what you want to do with the data it holds, and how you want to query it.

<Player>
 <Bio>
  <FirstName>Julius</FirstName>
  <LastName>Jones</LastName>            
  <Position>RB</Position>
  <Height>5-9</Height>
  <Born>08/14/1981</Born>
  <College>Notre Dame</College>
  <NFLExperience>Rookie</NFLExperience>
 </Bio>
 <Stats>
  <Season>
   <Year>2004</Year>
   <Team>
    <Name>Dallas Cowboys</Name>            
    <G>3</G>
    <GS>2</GS>
    <Rushing>
     <Att>68</Att>
     <Yards>247</Yards>
     <Avg>3.6</Avg>
     <Lg>33</Lg>
     <TD>2</TD>
     <Over20>1</Over20>
     <FD>12</FD>
    </Rushing>
    <Receiving>
     <No>3</No>
     <Yards>13</Yards>
     <Avg>4.3</Avg>
     <Lg>7</Lg>
     <TD>0</TD>
     <Over20>0</Over20>
     <Over40>0</Over40>
     <FD>1</FD>
    </Receiving>
   </Team>
  </Season>
 </Stats>
</Player>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yasir_MalikAuthor Commented:
Thank you all for your response.
What we ended up implementing was a just a regular class.  I guess we might as well have used structs.  Hashtables were not allowed as return types because they could not be serialized by SOAP.  Matrices ([,]) could not also be serialized, so I wasn't sure that the CollectionBase interface would work either.  Here is a sample of our XML output:
<?xml version="1.0" encoding="utf-8"?>
<PlayerInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
  <bio>
    <position>FS</position>
    <firstName>Roy</firstName>
    <lastName>Williams</lastName>
    <number>31</number>
    <height>6-0</height>
    <DOB>08/14/1980</DOB>
    <weight>226</weight>
    <college>Oklahoma</college>
    <experience>3</experience>
    <id>302238</id>
  </bio>
  <tables>
    <StatisticTable>
      <tableName>DEFENSIVE STATS</tableName>
      <columnNames>
        <string>Year</string>
        <string>G</string>
        <string>Total</string>
        <string>Tckl</string>
        <string>Ast</string>
        <string>Sacks</string>
        <string>Int</string>
        <string>Yds</string>
        <string>Avg</string>
        <string>Lg</string>
        <string>TD</string>
        <string>Pass Def</string>
      </columnNames>
      <teamsPlayedFor>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
      </teamsPlayedFor>
      <statistics>
        <ArrayOfDouble>
          <double>2002</double>
          <double>16</double>
          <double>99</double>
          <double>88</double>
          <double>11</double>
          <double>2</double>
          <double>5</double>
          <double>90</double>
          <double>18</double>
          <double>85</double>
          <double>2</double>
          <double>3</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2003</double>
          <double>16</double>
          <double>72</double>
          <double>55</double>
          <double>17</double>
          <double>2</double>
          <double>2</double>
          <double>69</double>
          <double>34.5</double>
          <double>39</double>
          <double>0</double>
          <double>6</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2004</double>
          <double>11</double>
          <double>62</double>
          <double>52</double>
          <double>10</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>6</double>
        </ArrayOfDouble>
      </statistics>
      <totalStatistics>
        <double>43</double>
        <double>233</double>
        <double>195</double>
        <double>38</double>
        <double>4</double>
        <double>7</double>
        <double>159</double>
        <double>22.7</double>
        <double>85</double>
        <double>2</double>
        <double>15</double>
      </totalStatistics>
    </StatisticTable>
    <StatisticTable>
      <tableName>FUMBLES</tableName>
      <columnNames>
        <string>Year</string>
        <string>G</string>
        <string>Fum</string>
        <string>Lost</string>
        <string>Fum Forced</string>
        <string>Own Rec</string>
        <string>Opp Rec</string>
        <string>Yards</string>
        <string>Tot Rec</string>
        <string>TD</string>
      </columnNames>
      <teamsPlayedFor>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
        <string>Dallas Cowboys</string>
      </teamsPlayedFor>
      <statistics>
        <ArrayOfDouble>
          <double>2002</double>
          <double>16</double>
          <double>0</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
          <double>3</double>
          <double>0</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2003</double>
          <double>16</double>
          <double>0</double>
          <double>0</double>
          <double>2</double>
          <double>0</double>
          <double>1</double>
          <double>0</double>
          <double>1</double>
          <double>0</double>
        </ArrayOfDouble>
        <ArrayOfDouble>
          <double>2004</double>
          <double>11</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
          <double>0</double>
        </ArrayOfDouble>
      </statistics>
      <totalStatistics>
        <double>43</double>
        <double>0</double>
        <double>0</double>
        <double>5</double>
        <double>0</double>
        <double>4</double>
        <double>0</double>
        <double>4</double>
        <double>0</double>
      </totalStatistics>
    </StatisticTable>
  </tables>
</PlayerInfo>

We are also implementing a cache.  Does anyone know why the asp_web.exe service just hangs when trying to read from a file?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.