WCF

时间:2016-08-10 11:23:39

标签: c# .net wcf serialization deserialization

我有一个类模板,它包含一些属性,其中一些属于DataSet。

我做了

KnownType[typeof(Template)]

我创建了一个

[Operation Contract] 
SerializableDictionary<string,Template> GetDicTemplate = New SerializableDictionary<string,Template>();

我的SerializableDictionary代码如下:

namespace Somthing
{
    [Serializable]
    [XmlRoot("dictionary")]

    public class SerializableDictionary<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable
    {
        public SerializableDictionary()
        {

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

        }

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

        }


        protected SerializableDictionary(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {

        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {

            return null;

        }



        public void ReadXml(System.Xml.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 != System.Xml.XmlNodeType.EndElement)
            {

                reader.ReadStartElement("item");



                reader.ReadStartElement("key");

                TKey key = (TKey)keySerializer.Deserialize(reader);

                reader.ReadEndElement();



                reader.ReadStartElement("value");

                TValue value = (TValue)valueSerializer.Deserialize(reader);

                reader.ReadEndElement();



                this.Add(key, value);



                reader.ReadEndElement();

                reader.MoveToContent();

            }

            reader.ReadEndElement();

        }



        public void WriteXml(System.Xml.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();



                writer.WriteStartElement("value");

                TValue value = this[key];

                valueSerializer.Serialize(writer, value);

                writer.WriteEndElement();



                writer.WriteEndElement();

            }

        }

        #endregion

        public virtual object Clone()
        {
            Type type = this.GetType();
            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = new MemoryStream())
            {
                formatter.Serialize(stream, this);
                stream.Seek(0, SeekOrigin.Begin);
                return formatter.Deserialize(stream);
            }
        }
    }

当我反序列化时,我收到错误,指出元素是一个无效的xmlnodetype,带有NetDispatcherFaultException

我尝试调试它,我发现它指向我的一个模板类属性,它是DataSet类型。

为了解决这个问题,我创建了一个新的MyDataContract类,其中我为数据集设置了getter_setter,但仍面临同样的问题。

如何解决此问题?

0 个答案:

没有答案