"Matt Kemmerer" <Matt
Ke******@discus sions.microsoft .comwrote in message
news:44******** *************** ***********@mic rosoft.com...
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 "associativ e 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="dictionar y" type="tns:Dicti onaryEntry"
minOccurs="0" maxOccurs="unbo unded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexTy pe name="Dictionar yEntry">
<xs:sequence>
<xs:element name="key" type="xs:int" />
<xs:element name="value" type="xs:dateTi me" />
</xs:sequence>
</xs:complexType>
</xs:schema>
This can be serialized from and deserialized into a class like this:
public class result {
[System.Xml.Seri alization.XmlEl ementAttribute( "dictionary ")]
public DictionaryEntry[] dictionary;
}
[System.Xml.Seri alization.XmlTy peAttribute(Nam espace="http://www.example.org/NewXMLSchema")]
public class DictionaryEntry {
[System.Xml.Seri alization.XmlEl ementAttribute( )]
public int key;
[System.Xml.Seri alization.XmlEl ementAttribute( )]
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