There was an error reflecting type - XML Serialization issue

前端 未结 4 540
轮回少年
轮回少年 2020-12-31 20:00

I have a Dictionary object which needs to be written into an XML file. The dictionary contains String type as Key and a custom class\'s Object (Deriving from System.Windows.

4条回答
  •  伪装坚强ぢ
    2020-12-31 20:18

    Guys.. With a little help on web I found a solution..

    I had to add another class

    [XmlRoot("dictionary")]
    public class SerializableDictionary: Dictionary, IXmlSerializable
    {      
    
        #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
    
    }
    

    Then used the SerializableDictionary object instead of normal Dictionary. Also in my CommonControls class I had to implement "IXmlSerializable" with following methods.

     #region IXmlSerializable Members
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }
    
        public void ReadXml(XmlReader reader)
        {
            throw new NotImplementedException();
        }
    
        public void WriteXml(XmlWriter writer)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    

    Now the whole thing is working ok. Thanks Everyone. !!!

提交回复
热议问题