468,107 Members | 1,390 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

Returning a Dictionary from a webservice

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.
Sep 13 '06 #1
1 10993
"Matt Kemmerer" <Matt Ke******@discussions.microsoft.comwrote in message
news:44**********************************@microsof t.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 "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("dict ionary")]
public DictionaryEntry[] dictionary;
}

[System.Xml.Serialization.XmlTypeAttribute(Namespac e="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
Sep 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Karuppasamy | last post: by
reply views Thread by Sharon | last post: by
4 posts views Thread by Daniel | last post: by
2 posts views Thread by Asim Qazi | last post: by
20 posts views Thread by Gustaf | last post: by
1 post views Thread by Ronchese | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.