English

How To: Create an empty XML file programmatically

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.

Procedure

To programmatically create an empty XML file:

  1. Create a new UIButtonControl in ArcCatalog.
    a. Click Tools > Customize to open the Customize dialog box.
    b. Click the Commands tab.
    c. Select UIControls from the Categories list box.
    d. Click New UIControl.
    e. Click UIButtonControl and Create.
    f. Drag the new UIButtonControl to the toolbar of your choice.
    g. 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'.

  2. Right-click the UIButtonControl and select View Source.
  3. Copy this code into the UIButtonControl's click event.

    Code:
    '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

  4. Close the Visual Basic Editor.
  5. Select the folder in which you want to create a new XML file.
  6. Click the UIButtonControl to run the code. A new XML file will appear within the folder.