Serialize inherited objects

Topics: serialization
Nov 5, 2008 at 6:58 AM

Hi, I am trying to serialize a graph with different type of nodes, all them inheriting from a base 'Node' class. I do serialize de graph but the properties of the 'child' nodes are not written on the XML file, only the base Node properties are. It makes sense because the graph is a <Node, Edge> graph and the GraphMLSerializer is also created as <Node,Edge>. What I would like to know is if there is a way to serialize this graph inlcuding all information of the nodes.


'Graph'

public class Graph: AdjacencyGraph<ARNode,Arc>
    {
        public Graph():base()
        {
            
        }

        public Graph(bool alloParallelEdges)
            : base(alloParallelEdges)
        {

        }

        public Graph(bool alloParallelEdges, int capacity)
            : base(alloParallelEdges, capacity)
        {

        }
    }

'Base Node'

public class ARNode: IIdentifiable
    {
        private string _id;

        #region Constructor
        
        public ARNode(string ID)
        {
            this._id = ID;
        }

        #endregion

        #region Public Properties

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        #endregion

        #region IIdentifiable Members

        string IIdentifiable.ID
        {
            get { return this.Id; }
        }

        #endregion
      public sealed class Factory : IIdentifiableVertexFactory<ARNode>
        {
            #region IIdentifiableVertexFactory<ARNode> Members

            public ARNode CreateVertex(string id)
            {
                return new ARNode(id);
            }

            #endregion
        }
    }

public class ARChildNode: ARNode
    {
        private string _code;

        public ARChildNode(string id)
            : base(id)
        {
        }

        [XmlAttribute("Code")]
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }
    }


//Code used to serialize

Graph myGraph = new Graph();

...

ARNode n0 = new ARNode("0");

ARChildNode n1 = new ARChildNode("1");
n1.Code = "code1";

ARChildNode n2 = new ARChildNode("2");
n1.Code = "code2";

ARNode n3 = new ARNode("3");

...

Arc a1 = new Arc(n0, n1);
Arc a2 = new Arc(n2, n3);

...

myGraph.AddVerticesAndEdge(a1);
myGraph.AddVerticesAndEdge(a2);

GraphMLSerializer<ARNode, Arc> serializer = new GraphMLSerializer<ARNode, Arc>();
XmlTextWriter writer = new XmlTextWriter(".\\graph1.xml",null);
serializer.Serialize(writer, graph);
Coordinator
Nov 6, 2008 at 3:47 AM
Yep that's a bug. Can you open a workitem on this?
Coordinator
Nov 6, 2008 at 5:06 AM
On a second thaught: this is not supported. GraphML requires to declare the node/edge attributes before hand. With this schema, inheritance is not supported since arbitrary attributes can be added in child classes.

You can use the .NET serialization support to dump the graph as binary.