public class FileService<T> { public void Save(T obj, string filePath) { XmlSerializer serializer = new XmlSerializer(typeof(T)); TextWriter writer = new StreamWriter(filePath); serializer.Serialize(writer, obj); writer.Close(); } public T Load(string filePath) { XmlSerializer serializer = new XmlSerializer(typeof(T)); TextReader reader = new StreamReader(filePath); var obj = (T)serializer.Deserialize(reader); reader.Close(); return obj; } } public class Parent { List<Child> children; [XmlArray("Children")] [XmlArrayItem("Child")] public List<Child> Children { get { if (children == null) children = new List<Child>(); return children; } set { children = value; } } public void AddChild(Child child) { child.Parent = this; Children.Add(child); } public static Parent Load(string path) { FileService<Parent> fileService = new FileService<Parent>(); var parent = fileService.Load(path); parent.Children.ForEach(child => child.Parent = parent); return parent; } } public class Child { int value; Parent parent; [XmlIgnore] public Parent Parent { get { return parent; } set { parent = value; } } [XmlElement("Value")] public int Value { get { return this.value; } set { this.value = value; } } public Child() { } public Child(int value) { this.value = value; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)