/// <summary>
 /// Add map service layer to map
 /// </summary>
 /// <param name="msi">Map service information</param>
 private void AddMapServiceLayer(esri.gpt.csw.MapServiceInfo msi)
 {
     switch (msi.ServiceType.ToUpper())
     {
         case "WMS": AddLayerWMS(msi, false);
             break;
         case "WCS": AddLayerWCS(msi, false);
             break;
         default: AddLayerArcIMS(msi);
             break;
     }
 }
        /// <summary>
        /// Add WCS map service layer to map
        /// </summary>
        /// <param name="msi">Map service information</param>
        private void AddLayerWCS(esri.gpt.csw.MapServiceInfo msi, Boolean fromServerUrl)
        {
            if (msi == null) { throw new ArgumentNullException(); }

            try
            {
                string _mapServerUrl = AppendQuestionOrAmpersandToUrlString(msi.Server);
                // append serviceParam to server url
                // todo: does msi.ServiceParam have a leading "?" or "&"?
                if (msi.ServiceParam.Length > 0 && !fromServerUrl)
                {
                    _mapServerUrl = _mapServerUrl + msi.ServiceParam;
                    _mapServerUrl = AppendQuestionOrAmpersandToUrlString(_mapServerUrl);
                }

                 // MapServiceInfo msi = new MapServiceInfo();
                        String[] s = _mapServerUrl.Trim().Split('?');

                        _mapServerUrl = s[0] + "?request=GetCapabilities&service=WCS";
                        CswClient client = new CswClient();
                        String response = client.SubmitHttpRequest("GET", _mapServerUrl, "");

                         XmlDocument xmlDocument = new XmlDocument();
                         try { xmlDocument.LoadXml(response); }
                         catch (XmlException xmlEx)
                         { }

                         XmlNodeList contentMetadata = xmlDocument.GetElementsByTagName("ContentMetadata");

                         if (contentMetadata != null && contentMetadata.Count > 0)
                         {
                             XmlNodeList coverageList = contentMetadata.Item(0).ChildNodes;

                             foreach (XmlNode coverage in coverageList) {

                                 XmlNodeList nodes = coverage.ChildNodes;

                                 foreach(XmlNode node in nodes)
                                 {
                                     if (node.Name.ToLower().Equals("name"))
                                     {
                                         _mapServerUrl = s[0] + "?request=GetCoverage&service=WCS&format=GeoTIFF&coverage=" + node.InnerText;

                                         try{
                                            String filePath  = client.SubmitHttpRequest("DOWNLOAD", _mapServerUrl, "");
                                            AddAGSService(filePath);

                                         } catch(Exception e){
                                             ShowErrorMessageBox("WCS service with no GeoTiff interface");
                                             return;
                                         }
                                     }
                                 }

                             }

                         }

                    }

            catch (Exception ex)
            {
              //  ShowErrorMessageBox(StringResources.AddWcsLayerFailed + "\r\n" + ex.Message);
            }
        }
        /// <summary>
        /// Add WMS map service layer to map
        /// </summary>
        /// <param name="msi">Map service information</param>
        private void AddLayerWMS(esri.gpt.csw.MapServiceInfo msi, Boolean fromServerUrl)
        {
            if (msi == null) { throw new ArgumentNullException(); }

            try
            {
                string url = AppendQuestionOrAmpersandToUrlString(msi.Server);
                // append serviceParam to server url
                // todo: does msi.ServiceParam have a leading "?" or "&"?
                if (msi.ServiceParam.Length > 0 || !fromServerUrl)
                {
                    url = url + msi.ServiceParam;
                    url = AppendQuestionOrAmpersandToUrlString(url);
                }
                IPropertySet propertySet = new PropertySetClass();
                propertySet.SetProperty("URL", url);

                IMxDocument mxDoc = (IMxDocument)m_application.Document;
                IMap map = (IMap)mxDoc.FocusMap;
                IActiveView activeView = (IActiveView)map;
                IWMSGroupLayer wmsGroupLayer = (IWMSGroupLayer)new WMSMapLayerClass();
                IWMSConnectionName wmsConnectionName = new WMSConnectionName();
                wmsConnectionName.ConnectionProperties = propertySet;

                // connect to wms service
                IDataLayer dataLayer;
                bool connected = false;
                try
                {
                    dataLayer = (IDataLayer)wmsGroupLayer;
                    IName connName = (IName)wmsConnectionName;
                    connected = dataLayer.Connect(connName);
                }
                catch (Exception ex)
                {
                    ShowErrorMessageBox (StringResources.ConnectToMapServiceFailed + "\r\n" + ex.Message);
                    connected = false;
                }
                if (!connected) return;

                // get service description out of the layer. the service description contains
                // inforamtion about the wms categories and layers supported by the service
                IWMSServiceDescription wmsServiceDesc = wmsGroupLayer.WMSServiceDescription;
                IWMSLayerDescription wmsLayerDesc;
                ILayer newLayer;
                ILayer layer;
                IWMSLayer newWmsLayer;
                IWMSGroupLayer newWmsGroupLayer;
             /*   for (int i = 0; i < wmsServiceDesc.LayerDescriptionCount; i++)
                {
                    newLayer = null;

                    wmsLayerDesc = wmsServiceDesc.get_LayerDescription(i);
                    if (wmsLayerDesc.LayerDescriptionCount == 0)
                    {
                        // wms layer
                        newWmsLayer = wmsGroupLayer.CreateWMSLayer(wmsLayerDesc);
                        newLayer = (ILayer)newWmsLayer;
                        if (newLayer == null) { throw new Exception(StringResources.CreateWmsLayerFailed); };
                    }
                    else
                    {
                        // wms group layer
                        newWmsGroupLayer = wmsGroupLayer.CreateWMSGroupLayers(wmsLayerDesc);
                        newLayer = (ILayer)newWmsGroupLayer;
                        if (newLayer == null) { throw new Exception(StringResources.CreateWmsGroupLayerFailed); }
                    }

                    // add newly created layer
                 //   if(wmsGroupLayer.get_Layer(i).Name != newLayer.Name)
                        wmsGroupLayer.InsertLayer(newLayer, 0);
                }*/

                // configure the layer before adding it to the map
                layer = (ILayer)wmsGroupLayer;
                layer.Name = wmsServiceDesc.WMSTitle;
                ExpandLayer(layer, true);
                SetLayerVisibility(layer, true);

                // add to focus map
                map.AddLayer(layer);

                return;
            }
            catch (Exception ex)
            {
                ShowErrorMessageBox (StringResources.AddWmsLayerFailed + "\r\n" + ex.Message);
            }
        }
        /// <summary>
        /// Add ArcIMS map service layer to map
        /// </summary>
        /// <param name="msi">Map service information</param>
        private void AddLayerArcIMS(esri.gpt.csw.MapServiceInfo msi)
        {
            if (msi == null) { throw new ArgumentNullException(); }
            try
            {
                IMxDocument mxDoc = (IMxDocument)m_application.Document;
                IMap map = (IMap)mxDoc.FocusMap;
                IActiveView activeView = (IActiveView)map;

                bool isAlreadyInMap = false;

                if (isAlreadyInMap)
                {
                    ShowErrorMessageBox (StringResources.MapServiceLayerAlreadyExistInMap);
                    return;
                }
                else
                {
                    //IIMSConnection imsConnection;
                    IIMSMapLayer imsMapLayer;

                    // create service description based on provided map service info
                    IIMSServiceDescription imsServiceDesc = new IMSServiceNameClass();
                    imsServiceDesc.URL = msi.Server;
                    imsServiceDesc.Name = msi.Service;
                    imsServiceDesc.ServiceType = acServiceType.acMapService;
                    // note: add as image map service
                    // todo: research on the relationship between MapServiceInfo.ServiceType and IIMSServiceDescription
                    #region acServiceType info
                    #endregion

                    // connect to service
                    try
                    {
                        imsMapLayer = new IMSMapLayerClass();
                        imsMapLayer.ConnectToService(imsServiceDesc);
                    }
                    catch (System.Runtime.InteropServices.COMException ex)
                    {
                        // note: when a site request usr/pwd to connect, user may choose "cancel". Then, imsMapLayer.ConnectToService()
                        //       would then throw an System.Runtime.InteropServices.COMException. Catch it but no need to display
                        //       any error message.
                        System.Diagnostics.Trace.WriteLine(ex.Message, "NoConnectionToService");
                        return;
                    }
                    catch (Exception ex)
                    {
                        ShowErrorMessageBox(StringResources.ConnectToMapServiceFailed + "\r\n" + ex.Message);
                        return;
                    }

                    ILayer layer = (ILayer)imsMapLayer;
                    ExpandLayer(layer, true);
                    SetLayerVisibility(layer, true);

                    // Add the layer
                    mxDoc.AddLayer(imsMapLayer);

                }
            }
            catch (Exception ex)
            {
                ShowErrorMessageBox(StringResources.AddArcimsLayerFailed + "\r\n" + ex.Message);
            }
        }