Re: Returning a Dictionary from a webservice



"Matt Kemmerer" <Matt Kemmerer@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:44306A2F-1831-44E7-83A9-ED6349A9C90A@xxxxxxxxxxxxxxxx
I'm trying to return a HashTable froma WebMethod. This fails with a not
supported method because HashTable implements IDictionary. What I'm doing
right now instead is stuffing my data into a DataSet and then extracting
my
data from the DataSet on the other side.

What I want to know is this: What is the easiest way to return something
similar to an associative array (HashTable) from a web service.

Always remember thtat web services are about platform independence, acheived
by sending and receiving XML, described by an XML Schema.

There is no such thing as "associative array" in XML Schema.

Instead, you probably want to return an array of key-value pairs. So, for
example, if your Hashtable had integer keys and a DateTime value, you might
define a schema like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
targetNamespace="http://www.example.org/NewXMLSchema";
xmlns:tns="http://www.example.org/NewXMLSchema";>
<xs:element name="return">
<xs:complexType>
<xs:sequence>
<xs:element name="dictionary" type="tns:DictionaryEntry"
minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="DictionaryEntry">
<xs:sequence>
<xs:element name="key" type="xs:int" />
<xs:element name="value" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:schema>

This can be serialized from and deserialized into a class like this:

public class result {

[System.Xml.Serialization.XmlElementAttribute("dictionary")]
public DictionaryEntry[] dictionary;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.example.org/NewXMLSchema";)]
public class DictionaryEntry {

[System.Xml.Serialization.XmlElementAttribute()]
public int key;

[System.Xml.Serialization.XmlElementAttribute()]
public System.DateTime value;
}


If a client needs to access this data in an associative manner, the client
can load the array elements into the client's version of an associative
array (assuming there is such a thing on the client platform).


Another way to think of this is that the important thing about your data is
that it's a collection of key-value pairs (and maybe that the keys are
unique, in which case that should be specified in the schema). Your client
shouldn't have to care whether you kept this data in a Hashtable or in a
relational database, or whether you kept it in pen and ink! You similarly
don't want to dictate how your client treats this data.

John


.



Relevant Pages

  • Re: Text terminal rendering design
    ... it is a fundamental element of the UI design. ... The management of that array's content by the client is quite a ... The array and the terminal are more tightly tied ... to do that because of the need to map into flavors of terminals. ...
    (comp.object)
  • Duck Typed Concepts for Ruby (was Re: A use case for an ordered hash)
    ... An Sequencable mixin can be defined that implements all sorts of operations such as append, concat, splice, sort, etc. ... extending an instance of Array with Sorted if the array is known to be sorted. ... Now returning to the thread at hand we can see that the difference between the associative array and hash hierarchies is that the hash hierarchy depends upon Hashable keys. ...
    (comp.lang.ruby)
  • Re: passing a NULL pointer from vb6 to an ATL method
    ... Is all this necessary if the client and server are in the same ... local/call_as is necessary fior marshaling. ... Dim array as Double ...
    (microsoft.public.vc.atl)
  • Re: passing a NULL pointer from vb6 to an ATL method
    ... Is all this necessary if the client and server are in the same apartment? ... Dim array as Double ...
    (microsoft.public.vc.atl)
  • Re: To serialize or not to serialize?
    ... array, sending smaller portions of data in several times... ... So, when the client asks for some data, the web service queries the ...
    (microsoft.public.dotnet.languages.csharp)