How to use LoadGraphFromStream function?

Apr 6, 2013 at 4:11 PM
Dear tcap479, I try to load graph from MemoryStream, I write a graphml file(exported from NodeXL) into a MemoryStream, then I use LoadGraphFromStream function. but I always get an exception-"Root element is missing". How can I use this function. My codes like this:

MemoryStream ms = new MemoryStream();
FileStream fs = new FileStream("D:\test.graphml",FileMode.Open);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes,0,(int)fs.Length);
ms.Write(bytes,0,(int)fs.Length);
GraphAdapter adapter = new GraphAdapter();
IGraph graph = adapter.LoadGraphFromStream(ms);
nodexlControl.Graph = graph;
Apr 6, 2013 at 4:20 PM
My aim is to generate a virtual graphMl file in Computer's RAM and draw it by NodeXl Control. So I must use LoadGraphFromStream function.
Apr 6, 2013 at 9:44 PM
Edited Apr 6, 2013 at 9:44 PM
Hi, Doug:

After writing to the MemoryStream, you need to "rewind" it back to its first byte before you pass it to the GraphMLGraphAdapter. If you don't do that, the MemoryStream looks like it's "empty" and the GraphMLGraphAdapter can't find the GraphML data in the stream.

The code after my signature compiled and ran successfully for me. It's the "Seek()" line that fixes your problem.

-- Tony


using System;
using Smrf.NodeXL.Core;
using Smrf.NodeXL.Adapters;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        MemoryStream ms = new MemoryStream();
        FileStream fs = new FileStream("C:\\Temp.graphml", FileMode.Open);
        byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 0, (int)fs.Length);
        ms.Write(bytes, 0, (int)fs.Length);
        ms.Seek(0, SeekOrigin.Begin);
        GraphMLGraphAdapter adapter = new GraphMLGraphAdapter();
        IGraph graph = adapter.LoadGraphFromStream(ms);
        // nodexlControl.Graph = graph;
    }
}
}
Apr 7, 2013 at 3:42 PM
Yeah! The problem is solved! 
Another question, How could I use the mouse to click on an edge just like clicking on a vertex and drag the edge?
Apr 7, 2013 at 7:46 PM
Doug:

NodeXL doesn't have a way to do that. The problem is that edges are so narrow, often just one pixel wide, that it would be difficult to position the mouse in just the right place to allow the edge to be clicked. Vertices, by contrast, usually have a large enough area to allow them to be easily clicked.

In our own application, the NodeXL Excel Template, we have a list of edges in their own worksheet, and selecting one or more rows in the worksheet selects the corresponding edges in the graph. Behind the scenes, the Template calls NodeXLControl.SetEdgeSelected() to accomplish this. I wonder if there is a similar edge list in your own application that could be used for selecting edges.

-- Tony
Apr 8, 2013 at 2:01 PM
I see. Thanks a lot! :P