Trouble iterating through ExtendedData

Oct 2, 2012 at 4:04 PM

How can I loop through the items Extendata Data in C# for a feature and get the field names, data types, data values, etc.  Once I get the ExtendedData object, all I see are the Data and SchemaData collections. 

Can you please provide some examples?

Coordinator
Oct 2, 2012 at 8:13 PM

There are different ways of storing extended data; one being as Data elements and the other as SchemaData elements. Those properties should therefore contain the data you are looking for (hopefully!)

Do you have any example Kml you are trying to parse?

Oct 2, 2012 at 10:19 PM

I am using the "ExtendedData+SchemaData" example KML file from the link you posted above.  And I think I have finally figured out how to get to the field names and types using this code:

foreach (Schema schema in file.Root.Flatten().OfType<Schema>())
{
    foreach (SimpleField field in schema.Fields)
    {
        string szName = field.Name;
        string szDataType = field.FieldType;

    }
}

As for getting the data values from a feature.  I think that this code will work:

foreach (Feature feature in file.Root.Flatten().OfType<Feature>())
{
    if (feature.ExtendedData != null)
    {
        foreach (Data data in feature.ExtendedData.Data)
        {
            string szName = data.Name;
            string szDataValue = data.Value;

        }

        foreach (SchemaData dataS in feature.ExtendedData.SchemaData)
        {
            foreach (SimpleData sd in dataS.SimpleData)
            {
                string szName = sd.Name;
                string szDataValue = sd.Text;

            }
        }
    }
}

Is this the best way to get to this information?  I first was trying to use the "ParseEntityFields" call in the EntityMapper class, but I did not see any way to get the information back using that class.

Also, if I want to find the data type for a SimpleData value, I may need to build my own collection of the Schema that are used in the KML file and lookup the data type myself? 

Thanks for your help.

Coordinator
Oct 6, 2012 at 10:11 AM

Sorry about the late reply. I think what you've posted is probably the best way to iterate over the fields.

The EntityMapper class is actually unrelated to extended data; it is for mimicking the behavior of the balloons in Google Earth and allows the replacement of entities inside text (see Entity Replacement for more details).

As for their types, you will probably need to create a dictionary of some sort mapping their name with their type. For simpler files, you might be able to use the StyleResolver.CreateResolvedStyle method to find the related style for the feature and iterate over its fields, but I'm not sure how performant this would be for larger files or with lots of styles.

Hope it helps.