HOW TO

Create an empty XML file programmatically

Last Published: April 25, 2020

Summary

Empty XML files are useful for creating metadata templates and for creating metadata documents describing resources that are not available online. For example, you might create an empty XML file and then use a metadata editor to add information describing a paper map, an atlas, or data that has been archived on tape.

Note: 
Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward

Procedure

To programmatically create an empty XML file:

  1. Create a new UIButtonControl in ArcCatalog.
    1. Click Tools > Customize to open the Customize dialog box.
    2. Click the Commands tab.
    3. Select UIControls from the Categories list box.
    4. Click New UIControl.
    5. Click UIButtonControl and Create.
    6. Drag the new UIButtonControl to the toolbar of your choice.
    7. Close the Customize dialog box.
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'How to create custom commands with VBA'.
  1. Right-click the UIButtonControl and select View Source.
  2. Copy the following code into the UIButtonControl's click event.
'Get the selected object and make sure it's a folder
Dim pGxApp As IGxApplication
Dim pGxObj As IGxObject
Set pGxApp = Application
Set pGxObj = pGxApp.SelectedObject
If Not (TypeOf pGxObj Is IGxFolder) Then Exit Sub

Dim sName As String
Dim lCnt As Long
sName = "New_XML_Document"
lCnt = 1

'Count the number of objects in the folder whose names begin 
'with "New_XML_Document"
Dim pGxCnt As IGxObjectContainer
Set pGxCnt = pGxObj
If pGxCnt.HasChildren Then
    Dim pGxChldEn As IEnumGxObject
    Dim pGxChld As IGxObject
    Set pGxChldEn = pGxCnt.Children
    On Error Resume Next
    Set pGxChld = pGxChldEn.Next
    Do While Not pGxChld Is Nothing
        If Left(pGxChld.name, 16) = sName Then lCnt = lCnt + 1
        Set pGxChld = pGxChldEn.Next
    Loop
End If
If lCnt > 1 Then sName = sName & "(" & lCnt & ")"

'Create a new GxMetadata object and give it a file name
Dim pGxFile As IGxFile
Set pGxFile = New GxMetadata
pGxFile.path = pGxObj.FullName & "\" & sName & ".xml"

'Generate metadata for the new GxObject
Dim pMD As IMetadata
Dim pXPS As IXmlPropertySet
Set pMD = pGxFile
Set pXPS = New XmlPropertySet
pMD.Metadata = pXPS

'Refresh the folder's contents
pGxObj.Refresh
  1. Close the Visual Basic Editor.
  2. Select the folder in which you want to create a new XML file.
  3. Click the UIButtonControl to run the code. A new XML file appears within the folder.

Article ID:000005765

Software:
  • ArcMap 9 x
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic