C#在xml中序列化递归对象

时间:2015-04-02 06:44:24

标签: c# xml serialization recursion

我想序列化一个递归对象结构:一个Element对象可以有很多连接。每个连接都必须有另一个元素。

抽象元素类:

public abstract class Element<Attribute>{
  public SerializableDictionary<String, Connection> connections;
  /** Other attributes and default constructor */
}

特殊元素:

public class SpecialElement : Element<Attribute>{...}

带递归的连接类:

public class Connection{
  //[XmlIgnoreAttribute] after removing this comment block, I will get an exception
  public Element<Attribute> target;
  /** Other attributes and default constructor */
}

我的SerializableDictionary在没有递归对象的情况下工作正常。

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, 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];
            var type = value.GetType();//new line added here
            valueSerializer = new XmlSerializer(type);//New line added here 
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
    #endregion
    public void AddRange(SerializableDictionary<TKey, TValue> otherDictionary)
    {
        if (otherDictionary == null)
            throw new ArgumentNullException("target");
        foreach (KeyValuePair<TKey, TValue> curEntry in otherDictionary)
            this.Add(curEntry.Key, curEntry.Value);
    }
}

删除注释块后,我得到以下System.InvalidOperationException:

Beim Generieren des XML-Dokuments ist ein Fehler aufgetreten.
bei System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
bei System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
bei System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o)

你可以帮我解决这个问题吗?

没人知道吗?这对我来说非常重要:(

0 个答案:

没有答案