Wednesday, December 28, 2016

Get FeatureLayer from GIS Layer Name in ArcGIS Explorer Application

Here is the code snippet to get the "FeatureLayer" from the GIS Layer Name in ArcGIS Explorer Application.


        public static FeatureLayer GetFeatureLayer(string GISLayerName)
        {
            FeatureLayer ToGoFeatureLayer = null;
            foreach (MapItem EachMapItem in ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Map.ChildItems)
            {
                if ((EachMapItem is FeatureLayer) && (EachMapItem.Name == GISLayerName))
                {
                    ToGoFeatureLayer = (FeatureLayer)EachMapItem;
                    break;
                }
            }
            return ToGoFeatureLayer;
        }

XML Serialization in C#


Simple Serialize and de-Serialize to and from XML. This sample takes a C# object and serializes to XML file. Also it reads contents from a XML file and de-serializes it.
 
Happy coding.
 
Cheers
Adam

public class CSerializeController
    {
        public bool Serialize(object InData, string FQXMLFileNametoSave)
        {
            bool ToGobool = false;
            StreamWriter sw = new StreamWriter(FQXMLFileNametoSave);
            System.Xml.Serialization.XmlSerializer TheXmlSerializer = new System.Xml.Serialization.XmlSerializer(InData.GetType());
            try
            {
                TheXmlSerializer.Serialize(sw, InData);
                ToGobool = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString() + " - Error in function Serialize");
            }
            finally
            {
                sw.Close();
                sw.Dispose();
            }
            return ToGobool;
        }
 
        public object DeSerialize(object InEmptyData, string FQXMLFileNametoLoad)
        {
            object ToGoobject = new object();
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(FQXMLFileNametoLoad);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(InEmptyData.GetType());
                ToGoobject = (object)serializer.Deserialize(sr);
            }
            catch (Exception ex)
            {
                ToGoobject = null;
                throw new Exception(ex.ToString() + " - Error in function DeSerialize");
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
            return ToGoobject;
        }
}
 
 

Get all file info from the folder using C# code


Here is the code snippet to get all the file information from a folder in the descending order of creation date and time.


var dirInfo = new DirectoryInfo(folderNametoSearch);

FileInfo[] filesInfo = dirInfo.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();

 

Get more details when an Error Exception is thrown in C#

When there is an issue in the C# code, the application will throw exceptions.

All these exceptions can be handled properly and also logged. Using Reflection, we can get the class name and the function name which caused the exception

Simply use the following catch block in all your try-catch functions and pass all the exception to the Utility class to handle it.

catch (Exception ex)
{

MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();

string methodName = method.Name;

string className = method.ReflectedType.Name;

UtilClass.HandleError(className, methodName, ex);

}
 
Happy Coding !

All Blogs so far ...