private void exportMapContent(string xml, string outputDir)
        {
            var doc = new XmlDocument();

            doc.LoadXml(xml);
            XmlNodeList nodelist = doc.ChildNodes;
            Map         map      = MapView.Active.Map;
            var         cm       = CIMMap.FromXml(xml);

            foreach (var layer in cm.Layers)
            {
                Layer lyr = map.FindLayer(layer, true);

                GetLayerNode(lyr, doc);
            }
            foreach (var st in cm.StandaloneTables)
            {
                StandaloneTable tbl = map.FindStandaloneTable(st);
                XmlNode         lnd;
                lnd = doc.SelectSingleNode("//StandaloneTables/String[text()='" + tbl.URI + "']");
                CIMService          cs       = new MapMemberService((MapMember)tbl);
                var                 xmlLayer = cs.GetDefinitionAsync();
                XmlDocumentFragment xfrag    = doc.CreateDocumentFragment();
                xfrag.InnerXml = xmlLayer.Result;
                XmlNode nd = xfrag.FirstChild;
                lnd.AppendChild(nd);
            }
            string str = TransformDocument(doc.InnerXml, System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), @"Stylesheet\CIMMap.xslt"), System.IO.Path.Combine(outputDir, MakeValidFileName(map.Name)));
        }
        private void SetMapMember(MapView mv)
        {
            bool changed = false;

            if (mv != null)
            {
                //Was a layer selected?
                var layer = mv.GetSelectedLayers().FirstOrDefault();
                if (layer != null)
                {
                    CIMService = new MapMemberService(layer);
                    changed    = true;
                }
                else
                {
                    //Was a table selected?
                    var table = mv.GetSelectedStandaloneTables().FirstOrDefault();
                    if (table != null)
                    {
                        CIMService = new MapMemberService(table);
                        changed    = true;
                    }
                    else
                    {
                        //A Map must have been selected
                        CIMService = new MapService(mv.Map);
                        changed    = true;
                    }
                }
            }
            if (!changed && CIMService != null)
            {
                CIMService = null;
            }
        }
        private void GetLayerNode(Layer lyr, XmlDocument doc)
        {
            Layer plyr = MapView.Active.Map.FindLayer(lyr.URI, true);


            MapMember mm = lyr;
            XmlNode   lnd;

            lnd = doc.SelectSingleNode("//Layers/String[text()='" + lyr.URI + "']");
            CIMService cs = new MapMemberService((MapMember)lyr);
            //var xmlLayer = GetlayerDefinitionAsync(lyr);
            var xmlLayer = cs.GetDefinitionAsync();
            XmlDocumentFragment xfrag = doc.CreateDocumentFragment();

            xfrag.InnerXml = xmlLayer.Result;
            XmlNode nd = xfrag.FirstChild;

            switch (lyr.GetType().Name)
            {
            case "GroupLayer":
                lnd.AppendChild(xfrag);
                GroupLayer glyr = (GroupLayer)lyr;
                foreach (var layer in glyr.Layers)
                {
                    lnd = doc.SelectSingleNode("//Layers/String[text()='" + lyr.URI + "']");
                    GetLayerNode(layer, doc);
                }
                break;

            case "FeatureLayer":
                var cfl = CIMFeatureLayer.FromXml(xmlLayer.Result);

                GetRenderer(cfl.Renderer, nd, ReportDir);
                lnd.AppendChild(nd);
                break;

            case "RasterLayer":
                var crl = CIMRasterLayer.FromXml(xmlLayer.Result);
                //GetRenderer(crl., nd, ReportDir);
                lnd.AppendChild(nd);
                break;

            case "StandaloneTable":
                lnd = doc.SelectSingleNode("//StandaloneTables/String[text()='" + lyr.URI + "']");
                lnd.AppendChild(nd);
                break;

            default:
                lnd.AppendChild(nd);
                break;
            }
        }
        private void Initialize()
        {
            ArcGIS.Desktop.Core.Events.ProjectClosingEvent.Subscribe((args) => {
                CIMService = null;//just do it when the project closes
                return(Task.FromResult(0));
            });

            #region Layout

            //ArcGIS.Desktop.Layouts.Events.LayoutChangedEvent.Subscribe((args) => {
            //  if (_cimService != null &&
            //      _cimService.ServiceType == CIMServiceType.LayoutElement) {
            //    if (_cimService.URI == args.Layout.URI) {
            //      CIMService = new LayoutService(args.Layout);
            //    }
            //  }
            //});

            ArcGIS.Desktop.Layouts.Events.LayoutClosedEvent.Subscribe((args) => {
                if (_cimService != null && (_cimService.ServiceType == CIMServiceType.Layout ||
                                            _cimService.ServiceType == CIMServiceType.LayoutElement))
                {
                    //assume we are showing the active layout or an element on it
                    if (FrameworkApplication.Panes.Count == 0 ||
                        FrameworkApplication.Panes.OfType <ILayoutPane>().Count() == 0)
                    {
                        lock (this) {
                            CIMService = null;
                        }
                    }
                }
            });

            ArcGIS.Desktop.Layouts.Events.LayoutViewEvent.Subscribe((args) => {
                if (args.Type == LayoutViewEventType.Initialized)
                {
                    if (LayoutView.Active != null)
                    {
                        lock (this) {
                            CIMService = null;
                            if (LayoutView.Active.Layout != null)
                            {
                                CIMService = new LayoutService(LayoutView.Active.Layout);
                            }
                        }
                    }
                }
            });


            //A layout or layout element has been selected in the TOC
            ArcGIS.Desktop.Layouts.Events.LayoutSelectionChangedEvent.Subscribe((args) => {
                if (args.Elements == null || args.Elements.Count() == 0)
                {
                    //The layout itself has been selected
                    var layout = LayoutView.Active?.Layout;
                    if (layout != null)
                    {
                        CIMService = new LayoutService(layout);
                    }
                }
                else
                {
                    //take the first element
                    CIMService = new LayoutElementService(args.Elements.First());
                }
            });

            ArcGIS.Desktop.Layouts.Events.ElementsPlacementChangedEvent.Subscribe((args) => {
                SetLayoutElement(args.ElementNames);
            });

            ArcGIS.Desktop.Layouts.Events.ElementsUpdatedEvent.Subscribe((args) => {
                SetLayoutElement(args.ElementNames);
            });

            ArcGIS.Desktop.Layouts.Events.PageChangedEvent.Subscribe((args) => {
                if (_cimService != null &&
                    _cimService.ServiceType == CIMServiceType.LayoutElement)
                {
                    if (LayoutView.Active != null)
                    {
                        if (LayoutView.Active.Layout.URI == _cimService.URI)
                        {
                            CIMService = new LayoutService(LayoutView.Active.Layout);
                        }
                    }
                }
            });

            #endregion
            #region Map
            //The map is deleted from the Project dockpane
            ArcGIS.Desktop.Mapping.Events.MapRemovedEvent.Subscribe((args) => {
                lock (this) {
                    if (CIMService != null && CIMService.ServiceType == CIMServiceType.Map)
                    {
                        if (args.MapPath == CIMService.URI)
                        {
                            //this affects our map
                            CIMService = null;//just do it when our map is removed
                        }
                    }
                }
            });
            //The MapPane is closed
            ArcGIS.Desktop.Mapping.Events.MapClosedEvent.Subscribe((args) => {
                lock (this) {
                    if (CIMService != null && CIMService.ServiceType == CIMServiceType.Map)
                    {
                        if (args.MapPane.MapView != null)//I think MapView is always valid here
                        {
                            if (args.MapPane.MapView.Map.URI == CIMService.URI)
                            {
                                //this affects our map
                                CIMService = null;//just do it when our map is closed
                            }
                        }
                        else if (FrameworkApplication.Panes.Count == 0 ||
                                 FrameworkApplication.Panes.OfType <IMapPane>().Count() == 0)
                        {
                            CIMService = null;//There are no more maps
                        }
                    }
                }
            });
            ArcGIS.Desktop.Mapping.Events.TOCSelectionChangedEvent.Subscribe((args) => {
                if (CIMViewerModule.IgnoreEvents)
                {
                    return;
                }
                CIMViewerModule.IgnoreEvents = true;
                SetMapMember(args.MapView);
                CIMViewerModule.IgnoreEvents = false;
            });
            ArcGIS.Desktop.Mapping.Events.ActiveMapViewChangedEvent.Subscribe((args) => {
                if (CIMViewerModule.IgnoreEvents)
                {
                    return;
                }
                CIMViewerModule.IgnoreEvents = true;
                if (args.OutgoingView != null)
                {
                    CIMService = null;
                }
                else if (args.IncomingView != null)
                {
                    SetMapMember(args.IncomingView);
                }
                CIMViewerModule.IgnoreEvents = false;
            });

            ArcGIS.Desktop.Mapping.Events.MapPropertyChangedEvent.Subscribe((args) => {
                if (CIMViewerModule.IgnoreEvents)
                {
                    return;
                }
                CIMViewerModule.IgnoreEvents = true;
                if (_cimService != null &&
                    _cimService.ServiceType == CIMServiceType.Map)
                {
                    //we have a map definition loaded
                    foreach (var map in args.Maps)
                    {
                        if (map.URI == _cimService.URI)
                        {
                            //our map is one of the maps that changed
                            //refresh it
                            CIMService = new MapService(map);
                            break;
                        }
                    }
                }
                CIMViewerModule.IgnoreEvents = false;
            });
            ArcGIS.Desktop.Mapping.Events.MapMemberPropertiesChangedEvent.Subscribe((args) => {
                if (CIMViewerModule.IgnoreEvents)
                {
                    return;
                }
                CIMViewerModule.IgnoreEvents = true;

                if (_cimService != null && _cimService.ServiceType == CIMServiceType.MapMember)
                {
                    foreach (var mm in args.MapMembers)
                    {
                        if (mm.URI == _cimService.URI)
                        {
                            //refresh
                            CIMService = new MapMemberService(mm);
                            break;
                        }
                    }
                }
                CIMViewerModule.IgnoreEvents = false;
            });
            ArcGIS.Desktop.Mapping.Events.LayersRemovedEvent.Subscribe((args) => {
                if (CIMViewerModule.IgnoreEvents)
                {
                    return;
                }
                CIMViewerModule.IgnoreEvents = true;

                if (_cimService != null && _cimService.ServiceType == CIMServiceType.MapMember)
                {
                    foreach (var layer in args.Layers)
                    {
                        if (layer.URI == _cimService.URI)
                        {
                            CIMService = null;
                        }
                    }
                }
                CIMViewerModule.IgnoreEvents = false;
            });
            #endregion Map
        }