Сериализация бинарного дерева XML атрибутами - C#
Формулировка задачи:
Добрый вечер. Задача следующая:
Есть класс-оболочка над обобщенным бинарным деревом Tree<T>
Этот класс является словарем переводов английских слов.
Бинарное дерево хранит KeyValuePair<TKey, TValue>:Tree<T>
Node<T>:
Помогите пожалуйста правильно расставить атрибуты для сериализации в XML.
При вызове следующего метода:
Возникает ошибка:
An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code.
Additional information: There was an error generating the XML document.
- Ключ - английское слово без учёта регистра.
- Значение Бинарное дерево Tree<KeyValuePair<TKey, TValue>>
- Ключ - "аббревиатура языка". Например, ru или fr.
- Значение - список слов, которые соответствуют слову на английском языке.
[Serializable]
public class KeyValuePair<TKey, TValue> : IComparable<KeyValuePair<TKey, TValue>>
where TKey : IComparable<TKey>
{
private TKey key;
private TValue value;
private KeyValuePair()
{
}
public KeyValuePair(TKey key, TValue value)
{
Key = key;
Value = value;
}
[XmlAttribute("Key")]
public TKey Key
{
get { return key; }
set { key = value; }
}
[XmlAttribute("Value")]
public TValue Value
{
get { return value; }
set { this.value = value; }
}
public int CompareTo(KeyValuePair<TKey, TValue> other)
{
return Key.CompareTo(other.Key);
}
} public class Tree<T> : IEnumerable, IEnumerable<T>
where T : IComparable<T>
{
private int count;
private Node<T> root;
private IComparer<T> comparer;
public Tree(T data) : this()
{
Root = new Node<T>(data);
Count = 1;
}
* * *
* * *
public int Count
{
get { return count; }
private set { count = value; }
}
[XmlElement("Root")]
public Node<T> Root
{
get { return root; }
private set { root = value; }
} [Serializable]
public class Node<T> : ISerializable
where T : IComparable<T>
{
private T data;
private Node<T> left;
private Node<T> right;
private Node<T> parent;
protected Node(SerializationInfo info, StreamingContext context)
{
Data = (T)info.GetValue("Data", typeof(T));
Left = (Node<T>)info.GetValue("Left", typeof(Node<T>));
Right = (Node<T>)info.GetValue("Right", typeof(Node<T>));
Parent = (Node<T>)info.GetValue("Parent", typeof(Node<T>));
}
public Node(T data)
{
Data = data;
}
[XmlElement("Data")]
public T Data
{
get { return data; }
private set { data = value; }
}
[XmlElement("Parent")]
public Node<T> Parent
{
get { return parent; }
set { parent = value; }
}
[XmlElement("Left")]
public Node<T> Left
{
get { return left; }
set { left = value; }
}
[XmlElement("Right")]
public Node<T> Right
{
get { return right; }
set { right = value; }
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Data", Data);
info.AddValue("Parent", Parent);
info.AddValue("Left", Left);
info.AddValue("Right", Right);
}
} public void SaveToFile(string path)
{
XmlSerializer format = new XmlSerializer(typeof(EnglishDictionary));
using (Stream fStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
format.Serialize(fStream, this);
}
}Решение задачи: «Сериализация бинарного дерева XML атрибутами»
textual
Листинг программы
public TKey Key
{
get { return key; }
set { key = value; }
}
public TValue Value
{
get { return value; }
set { this.value = value; }
}