Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / September 2006

Tip: Looking for answers? Try searching our database.

Returning a Dictionary from a webservice

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matt Kemmerer - 13 Sep 2006 22:24 GMT
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.
John Saunders - 13 Sep 2006 22:54 GMT
> I'm trying to return a HashTable froma WebMethod. This fails with a not
> supported method because HashTable implements IDictionary. What I'm doing
[quoted text clipped - 4 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.