Open .GraphML file with NodeXL Programatically.

Nov 2, 2010 at 4:24 PM

Hi there,

    I just wanted to open a .graphml file with NodeXL programatically. Let me know how to do it. I was just trying the following code on trial and error basis.

Please guide me on how to achieve this.

 Excel.Application excelApp = new Excel.Application();
 Excel.Workbook theWorkbook = excelApp.Workbooks.Open(
             @"C:\Program Files\Microsoft Research\Microsoft NodeXL Excel Template\NodeXLGraph.xltx",
0,true, 5,"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);

Excel.XmlMap xmlMap = null;
theWorkbook.XmlImport(@"C:\file.GraphML",
                out xmlMap, true,@"C:\newfile.xlsx");
excelApp.Visible = true;

Thanks,

Ranjeesh

Nov 2, 2010 at 5:13 PM

Ranjeesh:

It looks like you are trying to "drive" the NodeXL Excel template from code outside of Excel.  I know of no way to do that in a simple manner.  The problem is that the "import from GraphML" functionality is built into NodeXL, not Excel, and the NodeXL object model is not visible from outside of Excel.  The XmlImport method you are using is not a part of NodeXL, and it's not what NodeXL uses internally to import GraphML.

If you were creating your own application from scratch and wanted to import a GraphML file into a new NodeXL Graph object, then the GraphMLGraphAdapter class in NodeXL's class libraries would let you do that.  But I don't think that's what you're talking about.

-- Tony

Nov 3, 2010 at 11:21 AM

Nope....Thats what I was exactly looking for. Thanks alot Tony for you prompt reply and guidence. I used GraphMLGraphAdapter class and it worked fine. Here below is the code that I used.

using System;
using Microsoft.NodeXL.Adapters;
using Microsoft.NodeXL.Core;
using Microsoft.NodeXL.ExcelTemplate;
using Excel = Microsoft.Office.Interop.Excel;

namespace NodeXL
{
    public class NodeXLOpener
    {
        const string NodeXLTemplatePath = @"C:\Program Files\Microsoft Research\Microsoft NodeXL Excel Template\NodeXLGraph.xltx";
        Excel.Application excelApp = null;

        public void OpenGraphMLFile(string path)
        {
            try{
                IGraphAdapter graphMLGraphAdapter = new GraphMLGraphAdapter();
                IGraph graph = graphMLGraphAdapter.LoadGraphFromFile(path);

                ImportGraph(graph,
                    (String[])graph.GetRequiredValue(
                    ReservedMetadataKeys.AllEdgeMetadataKeys,
                    typeof(String[])),
                (String[])graph.GetRequiredValue(
                    ReservedMetadataKeys.AllVertexMetadataKeys,
                    typeof(String[]))
                );
            }
            catch(Exception ex){
                throw ex;
            }
            finally{
                excelApp.Visible = true;
            }
        }
       
        private void ImportGraph(IGraph graph,String[] edgeAttributes,String[] vertexAttributes)
        {
            GraphImporter graphImporter = new GraphImporter();
            try{
                graphImporter.ImportGraph(graph, edgeAttributes,
                    vertexAttributes, true,
                    this.GetNodeXLWorkBook());
            }
            catch(Exception ex){
                throw ex;
            }
            return;
        }      
       
        public Excel.Workbook GetNodeXLWorkBook()
        {
            excelApp = new Excel.Application();
            Excel.Workbook theWorkbook = null;
            try{
                theWorkbook = excelApp.Workbooks.Open(
                     NodeXLTemplatePath, 0, true, 5,
                      "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
                      0, true);
                return theWorkbook;
            }
            catch{
                return theWorkbook;}
        }
    }
}

Thanks a lot again!!!

- Ranjeesh Chandran