views:

57

answers:

1

Hi! I tried to pass a dictionary via WebServices. However it is not serializeable. So i wrote an Own Class that makes it serializeable:

using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;

namespace Platform
{
    public class SaDictionary<TKey, TValue> : Dictionary<TKey, TValue>,
                                              IXmlSerializable
    {
        #region Constructors
        public SaDictionary() : base() { }

        public SaDictionary(IDictionary<TKey, TValue> dictionary)
            : base(dictionary)  {  }

        public SaDictionary(IEqualityComparer<TKey> comparer)
            : base(comparer)  {  }

        public SaDictionary(int capacity)
            : base(capacity)  {  }

        public SaDictionary(IDictionary<TKey, TValue> dictionary,
                            IEqualityComparer<TKey> comparer)
            : base(dictionary, comparer)  {  }

        public SaDictionary(int capacity, IEqualityComparer<TKey> comparer)
            : base(capacity, comparer)  {  }

        //protected SaDictionary(SerializationInfo info,
        //                       StreamingContext context)
        //    : base(info, context)
        //{
        //}
        #endregion

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();

            if (wasEmpty)
                return;

            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");
                reader.ReadStartElement("key");
                TKey key = (TKey)keySerializer.Deserialize(reader);
                reader.ReadEndElement(); //key

                reader.ReadStartElement("value");
                TValue value = (TValue)valueSerializer.Deserialize(reader);
                reader.ReadEndElement(); //value
                this.Add(key, value);

                reader.ReadEndElement(); //item
                //   reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            foreach (TKey key in this.Keys)
            {
                writer.WriteStartElement("item");
                writer.WriteStartElement("key");
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement(); //key
                writer.WriteStartElement("value");
                TValue value = this[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement(); //value
                writer.WriteEndElement(); //item
            }
        }
    }
}

However i get an ArrayOfXElement back. Is there a way to cast it back to a Dictionary? greets

A: 

If you are using Visual Studio to generate your service proxies then by pressing the "Advanced" button you are presented with options to change the generated data types. For instance you can map collection types to a generic list or an observable collection, and for dictionaries you can essentially tell it to map to various data types.

This should help you out.

dParker
hi thx for the ansere but i still have some problems:now by doing:service references-> new service reference -> choose-> advanced i get a menu, where i can map the dictionary to lists or arrays.But i want to get a dictionary back...i got an error with not serializeable, so i wrote the class above, but now i get arrayofxelement