I would still use XML, but just write my own serializer. You can use the XML read / write classes in .Net to create a simple XML format:
<TopObject> <SubObject> <SubObject> etc. </SubObject> <SubObject> etc. </SubObject> </SubObject> <SubObject></SubObject> </TopObject>
I donβt know if you think this is understandable enough for a person, but better than creating a .Net serializer. It would be easy to read / write recursively.
Example:
Here is a simplified example that you can adapt. Suppose I have this class:
public class Node { public Node(String _SomeProperty) { this.SomeProperty = _SomeProperty; } public String SomeProperty; public List<Node> Children = new List<Node>(); }
Each Node has a property called SomeProperty . He may also have children; more Nodes in the Children property.
Here is the main from the Console application that creates some data from this class for serialization:
static void Main(string[] args) { // Make some data for testing Node baseObject = new Node("This is the base class"); List<Node> Children = new List<Node>(){ new Node("This is a child"), new Node("This is another child") }; baseObject.Children = Children; Node aSubChild = new Node("This is a child of a child"); baseObject.Children[0].Children = new List<Node>() { aSubChild }; // Serialize XmlWriter writer = XmlWriter.Create("test.xml"); writer.WriteStartDocument(); RecursivelySerialize(ref writer, baseObject); writer.Flush(); }
It calls a method called RecursivelySerialize , which does this actual work:
private static void RecursivelySerialize(ref XmlWriter writer, Node sc) { writer.WriteStartElement("Node"); writer.WriteElementString("SomeProperty", sc.SomeProperty); if (sc.Children.Count > 0) { writer.WriteStartElement("Nodes"); foreach (Node scChild in sc.Children) RecursivelySerialize(ref writer, scChild); writer.WriteEndElement(); } writer.WriteEndElement(); }
This method is not complicated. To improve it, you can use Reflection to dynamically serialize any type of class. Here is the result I got (formatted nicely) when running the above code:
<?xml version="1.0" encoding="utf-8"?> <Node> <SomeProperty>This is the base class</SomeProperty> <Nodes> <Node> <SomeProperty>This is a child</SomeProperty> <Nodes> <Node> <SomeProperty>This is a child of a child</SomeProperty> </Node> </Nodes> </Node> <Node> <SomeProperty>This is another child</SomeProperty> </Node> </Nodes> </Node>