1

I'm working with a graph dataset that could have edges of different logical meaning, like that:

"Bavaria" -[PART_OF]-> "Germany"
"Germany" -[NEIGHBOR_OF]-> "France"

I represent this domain by using the QuickGraph.TaggedEdge where TTag generic parameter is a string:

QuickGraph.BidirectionalGraph<IGeographicalUnit, TaggedEdge<IGeographicalUnit, string>>

This works perfectly until the moment I try to serialize my graph to .graphml form:

using (var w = XmlWriter.Create(@"C:\Temp\data.graphml"))
{
    _graph.SerializeToGraphML<IGeographicalUnit, TaggedEdge<IGeographicalUnit, string>, BidirectionalGraph<IGeographicalUnit, TaggedEdge<IGeographicalUnit, string>>>(w);
}

Serialized edge data doesn't contain any tag information, only source and target:

<edge id="0" source="0" target="1" />

What I want is something like:

<edge id="0" source="0" target="1" tag="PART_OF" />

So, the question is, how can I enforce this tag to be serialized?

4

1 回答 1

1

为了解决这个问题,我创建了自己的边缘实现,与TaggedEdge上面提到的非常相似:

public class DataEdge<TVertex> : Edge<TVertex>
{
    [XmlAttribute("Tag")]
    public string Tag { get; set; }

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

    public DataEdge(TVertex source, TVertex target, string tag)
        : this(source, target)
    {
        Tag = tag;
    }
}

然后我TaggedEdge用我的DataEdge. 之后,序列化产生以下 XML 输出:

<edge id="0" source="0" target="1">
  <data key="Tag">PART_OF</data>
</edge>

问题已解决,但我希望存在不涉及编写自己的Edge实现的另一种解决方案。

于 2014-03-31T05:50:00.780 回答