How to access maps and layers via the MapDocument


Summary The MapDocument class is a utility class for reading and modifying map documents (.mxds). This topic demonstrates how to open a map document and loop through all its maps and layers.

Accessing maps and layers via the MapDocument 

In the following code example, the names of these objects are reported to the console. Do the following steps to access maps and layers:
  1. Create a MapDocument.
[C#]
IMapDocument pMapDocument = new MapDocumentClass();
[VB.NET]
Dim pMapDocument As IMapDocument = New MapDocumentClass()
  1. Enumerate the maps in the MapDocument and print their names to the console. See the following code example:
[C#]
if (pMapDocument.get_IsMapDocument(path))
{
    pMapDocument.Open(path, null);
    IMap pMap;
    for (int i = 0; i <= pMapDocument.MapCount - 1; i++)
    {
        pMap = pMapDocument.get_Map(i);
        Console.WriteLine(pMap.Name);
        IEnumLayer pEnumLayer = pMap.get_Layers(null, true);
        pEnumLayer.Reset();
        ILayer pLayer = pEnumLayer.Next();
        while (pLayer != null)
        {
            Console.WriteLine(pLayer.Name);

            pLayer = pEnumLayer.Next();
        }

    }
}
[VB.NET]
If pMapDocument.IsMapDocument(Path) Then
    pMapDocument.Open(Path, Nothing)
    Dim pMap As IMap
    Dim i As Integer
    For i = 0 To pMapDocument.MapCount - 1 Step i + 1
        pMap = pMapDocument.Map(i)
        Console.WriteLine(pMap.Name)
        Dim pEnumLayer As IEnumLayer = pMap.Layers(Nothing, True)
        pEnumLayer.Reset()
        Dim pLayer As ILayer = pEnumLayer.Next()
        While Not pLayer Is Nothing
            Console.WriteLine(pLayer.Name)
            
            pLayer = pEnumLayer.Next()
        End While
        
    Next
End If






Additional Requirements
  • Path—A string that points to the map document you want to open.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime