Question

I've been looking for this one in a while but coudn't find anything.

I am trying to deserialize graph from GraphML using Quickgraph in C#. Here's the class I use to represent Vertex and Edge

[Serializable]
public class Room
{
    public Room(int id, double x, double y)
    {
        this.Ids = id;
        this.x = x;
        this.y = y;
    }

    [XmlAttribute("id")]
    public int Ids { get; set; }

    [XmlAttribute]
    public double x { get; set; }

    [XmlAttribute]
    public double y { get; set; }

}

[Serializable]
public class HouseEdge<TVertex> : Edge<TVertex>
{
    public string Name { get; set; }

    public HouseEdge(TVertex source, TVertex target)
        : base(source, target)
    {
    }
}

And I am trying to deserialize the code using method DeserializeFromGraphML:

XmlReader xreader = getXMLReader("//house.xml");
IdentifiableVertexFactory<Room> ivf = new IdentifiableVertexFactory<Room>(makeTest);
IdentifiableEdgeFactory<Room, HouseEdge<Room>> ief = new IdentifiableEdgeFactory<Room, HouseEdge<Room>>(makeTest2);
graph.DeserializeFromGraphML<Room, HouseEdge<Room>, AdjacencyGraph<Room, HouseEdge<Room>>>(xreader, ivf, ief);
xreader.Close();

And here's the code for makeTest and makeTest2:

private Room makeTest(string cos)
{
    MessageBox.Show(cos);
    return new Room(11, 12.0, 13.0);
}

private HouseEdge<Room> makeTest2(Room one, Room two, string cos)
{
    MessageBox.Show(cos);
    return new HouseEdge<Room>(one, two);
}

So - in my XML file I got 2 vertices - (0,0,0) and (1,1,1) and edge between them. When I deserialize them edge is just fine but vertices goes (0,12,0) and (1,12,1). There has to be something that I'm missing (twelve comes from makeTest method) but cos variable only has number zero and one just as position of vertex in the "Vertices" of graph (that's what I'm showing in MessageBox)

I know it can be complex at first glance but maybe it's really simple? Am I missing something?

Thanks in advance!

Was it helpful?

Solution

According to my research, this is a bug in library (or there are more people that can't use this library properly). Here is one of the discussions at library forums where other pewople experience the same issues as me.

Because of this I created a code in LINQ to XML and did it manually - it work's just like navigating through normal XML. One thing to remember is to use proper XNames, because using a string as element name simply doesn't work. For example:

XName XNgraph = XName.Get("graph", "http://graphml.graphdrawing.org/xmlns");
var edges = xelement.Elements(XNgraph)

Good luck if you've got this same problem as me - hope I did help you a little :)

OTHER TIPS

I discovered this same issue today, and found a different workaround before finding this post, in case it is useful to anyone.

I noticed as I was examining the XML output by the GraphMLSerializer, that it was deserializing properly after I would pretty-print the xml (I was using a Notepad++ plugin to make the XML easier to read). So I actually worked around it by making a slight change to my serializing code, adding a few settings to my XmlWriter so that it would pretty-print the XML it emits:

using (XmlWriter writer = XmlWriter.Create(filename, new XmlWriterSettings { 
        Indent = true,
        WriteEndDocumentOnClose = false
    })) {

    this.SerializeToGraphML<StoryNode, Choice, StoryArcGraph>(writer, 
        v => v.Id,
        e => e.Id
        );
}

My project is only ever loading XML emitted by itself, and the extra formatting characters are not an overwhelming amount of overhead, so I found this to be an acceptable workaround.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top