Export Attributes to a XML-File

472
4
04-20-2011 07:52 AM
New Contributor
Hi ESRI-Forum,

I'm looking for a code-sample for exporting the attribute table of a layer to a XML-File. Would be nice, if somebody can send me a hint!

Kai
Reply
0 Kudos
4 Replies
MVP Frequent Contributor
Hi ESRI-Forum,

I'm looking for a code-sample for exporting the attribute table of a layer to a XML-File. Would be nice, if somebody can send me a hint!

Kai



I am unsure of any ArcObjects that could do this for you.  Perhaps there is and someone will reply, but otherwise I'd suggest writing the attributes to an ADO.NET DataTable that is added to an ADO.NET DataSet.  From there you could simply write out the schema of the DataSet to an .xml file with:

Dim ds As New DataSet()
ds.DataSetName = "dsName"
Dim filename As String = ds.DataSetName.ToString & ".xml"
ds.WriteXmlSchema(filename)
Reply
0 Kudos
MVP Frequent Contributor
If you are not into the the whole ADO.NET stuff, then you might be able to use something from this sample:
http://edndoc.esri.com/arcobjects/9.0/default.asp?URL=/arcobjects/9.0/Samples/Geodatabase/Creating_a...

I have not attempted to implement this particular sample.
Reply
0 Kudos
Occasional Contributor II
These are all good suggestions from James. I would also like to mention that NIM011676 has been logged requesting a GP tool for this.
Reply
0 Kudos
MVP Frequent Contributor
Just to follow up, here is an example of how you can accomplish what you want to do.  Again, there might very well be a simple ArcObjects solution here --- howver most of my GIS apps are really data-centric and I do a lot of work implementing ADO.NET stuff into my GIS applications.  So that is the way I am going to create any .xml conversion process.

First, you should go to the ArcScripts and save a copy of one of my uploads I did there.  This is a slick little toolset that converts your IFeatureLayer to an ADO.NET DataTable and can be used for a wide variety of scenarios.  I particularly like to use this functionality to display data in DataGridViews and the like.

http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=675318D8-1422-2418-8814-...

But, I've pasted the relevent Function below that you'll need out of that script.


Private Function ConvertToADONETDataTableFromLayer(ByVal inLayer As IFeatureLayer) As DataTable

        Dim tmpDT As New DataTable("tmpDT")
        Dim column As DataColumn

        Dim pTable As ITable = inLayer.FeatureClass
        Dim pFields As IFields = pTable.Fields
        Dim pCur As ICursor = pTable.Search(Nothing, False)

        For c = 0 To pCur.Fields.FieldCount - 1
            column = New DataColumn()
            column.ColumnName = (pFields.Field(c).Name)
            If pFields.Field(c).Type = esriFieldType.esriFieldTypeString Then
                column.DataType = System.Type.GetType("System.String")
            ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeInteger Then
                column.DataType = System.Type.GetType("System.Int32")
            ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDouble Then
                column.DataType = System.Type.GetType("System.Double")
            ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeDate Then
                column.DataType = System.Type.GetType("System.DateTime")
            ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeSingle Then
                column.DataType = System.Type.GetType("System.Single")
            ElseIf pFields.Field(c).Type = esriFieldType.esriFieldTypeBlob Then
                column.DataType = System.Type.GetType("System.Byte")
            End If
            column.ReadOnly = False
            tmpDT.Columns.Add(column)
        Next

        Dim pRow As IRow = pCur.NextRow
        Dim newRow As DataRow

        Do Until pRow Is Nothing
            newRow = tmpDT.NewRow()
            newRow.BeginEdit()
            For cols = 0 To pCur.Fields.FieldCount - 1
                newRow(cols) = pRow.Value(pRow.Fields.FindField(pFields.Field(cols).Name))
            Next
            newRow.EndEdit()
            tmpDT.Rows.Add(newRow)
            tmpDT.AcceptChanges()

            pRow = pCur.NextRow
        Loop

        Return tmpDT
    End Function



Then use this to build the .xml file:


      
 
        Dim pDoc As IMxDocument
        Dim pMap As IMap
        pDoc = m_pApp.Document
        pMap = pDoc.FocusMap

        Dim pLayer as IFeatureLayer = pMap.Layer(0) 'just get the first layer for simplicity/example

        'Create the ADO.NET DataTable
        Dim adoDataTable As DataTable = ConvertToADONETDataTableFromLayer(pLayer)

        'Setup the ADO.NET Dataset and add the newly created DataTable
        Dim ds As New DataSet()
        ds.DataSetName = "dsToConvertToXML"
        ds.Tables.Add(adoDataTable)

        'Now write the DataTable's schema to an .xml file
        'remember, this is *really* the schema of the IFeatureLayer because we converted it to an
        'ADO.NET DataTable!

        Dim filename As String = ds.DataSetName.ToString & ".xml"
        ds.WriteXmlSchema(filename)



So from the test that I ran, this adds the .xml file to the directory where your VS.NET project folder is located.  There are probably ways to manipulate exactly where to place this .xml file, but you'll have to look into that.

Good luck and let me know if this helps!


Here's the sample schema that I build from a point layer I had run this on:

[HTML]<?xml version="1.0" standalone="yes"?>
<xs:schema id="dsToConvertToXML" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="dsToConvertToXML" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="tmpDT">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Shape" type="xs:string" minOccurs="0" />
              <xs:element name="OBJECTID" type="xs:string" minOccurs="0" />
              <xs:element name="zone" type="xs:string" minOccurs="0" />
              <xs:element name="Location" type="xs:int" minOccurs="0" />
              <xs:element name="timestart" type="xs:dateTime" minOccurs="0" />
              <xs:element name="FlagType" type="xs:string" minOccurs="0" />
              <xs:element name="Latitude" type="xs:double" minOccurs="0" />
              <xs:element name="Longitude" type="xs:double" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>[/HTML]
Reply
0 Kudos