Converts a graph to and from a GraphML file.
A good introduction to GraphML can be found in the GraphML Primer:

http://graphml.graphdrawing.org/primer/graphml-primer.html

Here is a sample GraphML file that can be converted. It represents a graph with three vertices and two edges.

<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns"> <key id="EdgeWidth" for="edge" attr.name="Width" attr.type="double"> <default>1.5</default> </key> <key id="VertexColor" for="node" attr.name="Color" attr.type="string" /> <key id="LatestPostDate" for="node" attr.name="Latest Post Date" attr.type="string" /> <graph edgedefault="undirected"> <node id="V1"> <data key="VertexColor">red</data> </node> <node id="V2"> <data key="VertexColor">orange</data> </node> <node id="V3"> <data key="VertexColor">blue</data> </node> <edge source="V1" target="V2"> <data key="LatestPostDate">2009/07/05</data> </edge> <edge source="V3" target="V2"> <data key="EdgeWidth">2.5</data> <data key="LatestPostDate">2009/07/12</data> </edge> </graph> </graphml>

Edge and vertex attributes, which GraphML calls "GraphML-attributes," are supported by this class. When loading a graph, if an edge or vertex has a GraphML-attribute, it gets added to the metadata of the or IVertex. The metadata key is the GraphML-attribute's attr.name value and the metadata value is the GraphML-attribute's value. When saving a graph, every metadata value on every edge and vertex gets converted to a GraphML-attribute in the saved GraphML.

To make it possible for the caller to determine which metadata keys were added to the graph's edges and vertices, the LoadXX methods add and keys to the returned graph. The key values are of type String[].

If there is an optional "description" attribute on the "graph" XML node, the LoadXX methods copy its value to a key on the returned graph.

If there is an optional "suggestedFileNameNoExtension" attribute on the "graph" XML node, the LoadXX methods copy its value to a key on the returned graph.

When saving a graph, the caller must add and keys to the graph before calling IGraphAdapter.SaveGraph(IGraph, Stream).

Наследование: GraphAdapterBase, IGraphAdapter
        private void loadGraphMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //GraphMLGraphAdapter test, load
            DialogResult d = cdbOpenGraphML.ShowDialog();
            if (d != DialogResult.Cancel)
            {
                this.Cursor = Cursors.WaitCursor;
                Application.DoEvents();

                try
                {
                    System.IO.StreamReader file = new System.IO.StreamReader(cdbOpenGraphML.FileName);
                    string line = file.ReadToEnd();
                    oGraphMLGraphAdapter = new GraphMLGraphAdapter();
                    
                    layoutControl1.Graph = oGraphMLGraphAdapter.LoadGraphFromString(line); 
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                this.Cursor = Cursors.Default;
                //Application.DoEvents();
            }
        }
Пример #2
0
        public void saveGraph(IGraph graph)
        {
            GraphMLGraphAdapter graphMlAdapter = new Smrf.NodeXL.Adapters.GraphMLGraphAdapter();

            FileStream fs = File.Create("graph.graphml");

            graphMlAdapter.SaveGraph(graph, fs);
        }
Пример #3
0
        public IGraph Generator()
        {
            writeFile();

            GraphMLGraphAdapter graphMlAdapter = new Smrf.NodeXL.Adapters.GraphMLGraphAdapter();
            IGraph graph = graphMlAdapter.LoadGraphFromFile("GraphAsGraphML.txt");

            return(graph);
        }
Пример #4
0
        public IGraph Generator(bool facebook)
        {
            GraphMLGraphAdapter graphMlAdapter = new Smrf.NodeXL.Adapters.GraphMLGraphAdapter();
            IGraph graph;

            if (facebook)
            {
                graph = graphMlAdapter.LoadGraphFromFile("facebookgraph.graphml");
            }
            else
            {
                graph = graphMlAdapter.LoadGraphFromFile("sndata.graphml");
            }


            return(graph);
        }
Пример #5
0
    TryGetGraphFromGraphDataProvider
    (
        Object graphDataProvider,
        out IGraph graph
    )
    {
        Debug.Assert(graphDataProvider != null);

        Debug.Assert(graphDataProvider is IGraphDataProvider2 ||
            graphDataProvider is IGraphDataProvider);

        graph = null;
        GraphMLGraphAdapter oGraphMLGraphAdapter = new GraphMLGraphAdapter();

        if (graphDataProvider is IGraphDataProvider2)
        {
            String sPathToTemporaryFile = null;

            if ( !( (IGraphDataProvider2)graphDataProvider )
                .TryGetGraphDataAsTemporaryFile(out sPathToTemporaryFile) )
            {
                return (false);
            }

            try
            {
                graph = oGraphMLGraphAdapter.LoadGraphFromFile(
                    sPathToTemporaryFile);
            }
            finally
            {
                File.Delete(sPathToTemporaryFile);
            }
        }
        else
        {
            String sGraphDataAsGraphML;

            if ( !( (IGraphDataProvider)graphDataProvider ).TryGetGraphData(
                out sGraphDataAsGraphML) )
            {
                return (false);
            }

            graph = oGraphMLGraphAdapter.LoadGraphFromString(
                sGraphDataAsGraphML);
        }

        return (true);
    }
        private void youTuBeVideoCrawlerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File.Delete(TempXmlFileName);

            GetGraphData(new YouTubeVideoNetworkGraphDataProvider());
            if (File.Exists(TempXmlFileName))
            {
                System.IO.StreamReader file = new System.IO.StreamReader(TempXmlFileName);
                string line = file.ReadToEnd();
                file.Close();
                GraphMLGraphAdapter oGraphMLGraphAdapter = new GraphMLGraphAdapter();
                layoutControl1.SetAndShowGraph(oGraphMLGraphAdapter.LoadGraphFromString(line));
            }
            else
            {
                MessageBox.Show("Error: 請重新選取youTuBe Video!" , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                m_oGraph = new Graph();
                layoutControl1.SetAndShowGraph(m_oGraph);
            }
        }
 SetUp()
 {
     m_oGraphAdapter = new GraphMLGraphAdapter();
     m_sTempFileName = Path.GetTempFileName();
 }
 private void youtubeCrawlerToolStripMenuItem_Click(object sender, EventArgs e)
 {
     GetGraphData(new YouTubeUserNetworkGraphDataProvider());
     System.IO.StreamReader file = new System.IO.StreamReader(TempXmlFileName);
     string line = file.ReadToEnd();
     GraphMLGraphAdapter oGraphMLGraphAdapter = new GraphMLGraphAdapter();
     layoutControl1.Graph = oGraphMLGraphAdapter.LoadGraphFromString(line); 
 }
    OpenObject
    (
        String sFileName,
        out Object oObject
    )
    {
        Debug.Assert( !String.IsNullOrEmpty(sFileName) );
        Debug.Assert( File.Exists(sFileName) );
        AssertValid();

        oObject = null;

        // Use a graph adapter to create a graph from the file.

        IGraphAdapter oGraphMLGraphAdapter = new GraphMLGraphAdapter();

        oObject = oGraphMLGraphAdapter.LoadGraphFromFile(sFileName);
    }
    GetGraphML
    (
        Microsoft.Office.Interop.Excel.Workbook oWorkbook
    )
    {
        Debug.Assert(oWorkbook != null);

        String sGraphML = null;

        // The graph owned by the NodeXLControl can't be used, because it
        // doesn't include all the edge and vertex column data needed for
        // GraphML.  Instead, read the graph from the workbook and include all
        // the necessary data.

        ReadWorkbookContext oReadWorkbookContext = new ReadWorkbookContext();
        oReadWorkbookContext.ReadAllEdgeAndVertexColumns = true;

        WorkbookReader oWorkbookReader = new WorkbookReader();

        IGraph oGraphForGraphML = oWorkbookReader.ReadWorkbook(
            oWorkbook, oReadWorkbookContext);

        GraphMLGraphAdapter oGraphMLGraphAdapter = new GraphMLGraphAdapter();

        using ( MemoryStream oMemoryStream = new MemoryStream() )
        {
            oGraphMLGraphAdapter.SaveGraph(oGraphForGraphML, oMemoryStream);
            oMemoryStream.Position = 0;

            using ( StreamReader oStreamReader =
                new StreamReader(oMemoryStream) )
            {
                sGraphML = oStreamReader.ReadToEnd();
            }
        }

        return (sGraphML);
    }