Problems with KML

Jun 12, 2009 at 2:15 PM
Edited Jun 12, 2009 at 2:35 PM

Hello,

I'm using the binaries of the latest build (2.8.4) to generate classes based on the OGC KML 2.2 schema.  I can successfully create the schema, but then XML serialization doesn't work.  There are a number of problems I ran into.  I want to enumerate what I did to get it to work, but would like these issues to be addressed (whether it was something I did wrong) and/or fixed (if it is indeed a bug).

You can try running it yourself--the schema is located here: http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd, and it depends on the atom author-link schema which is here: http://schemas.opengis.net/kml/2.2.0/atom-author-link.xsd

I ran Xsd2Code.exe with the following switches: /dbg /sc /xa

I try to create a new empty KML file like so:

KmlType kml = new KmlType();
DocumentType doc = new DocumentType();
kml.Item = doc;

using ( XmlWriter writer = XmlTextWriter.Create( outXgpsFile ) )
{
	XmlSerializer s = new XmlSerializer( typeof( KmlType ) );
	s.Serialize( writer, kml );
}

and I get this error (PROBLEM 1):

Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'Kml2_2.KmlType'. --->
System.InvalidOperationException: There was an error reflecting property 'NetworkLinkControl'. --->
System.InvalidOperationException: There was an error reflecting type 'Kml2_2.NetworkLinkControlType'. --->
System.InvalidOperationException: There was an error reflecting property 'Update'. --->
System.InvalidOperationException: There was an error reflecting type 'Kml2_2.UpdateType'. --->
System.InvalidOperationException: There was an error reflecting property 'Items'. --->
System.InvalidOperationException: There was an error reflecting type 'Kml2_2.ChangeType'. --->
System.InvalidOperationException: There was an error reflecting property 'Items'. --->
InvalidOperationException: Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'.
Please use array of System.Collections.Generic.List`1[[Kml2_2.ItemsChoiceType1, Utilities, Version=1.0.2.0, Culture=neutral, PublicKeyToken=null]].

I had no idea what was going on there, but I knew that I didn't really need NetworkLinkControl, so I got commented that out of KmlType (the private variable, the property, and the initialization in the constructor).

When I run the code again, I get this error (PROBLEM 2):

Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'Kml2_2.KmlType'. --->
System.InvalidOperationException: There was an error reflecting property 'Item'. --->
System.InvalidOperationException: There was an error reflecting type 'Kml2_2.PlacemarkType'. --->
System.InvalidOperationException: There was an error reflecting property 'author'. --->
System.InvalidOperationException: There was an error reflecting type 'Kml2_2.atomPersonConstruct'. --->
System.InvalidOperationException: There was an error reflecting property 'Items'. --->
System.InvalidOperationException: Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'.
Please use array of System.Collections.Generic.List`1[[Kml2_2.ItemsChoiceType, Utilities, Version=1.0.2.0, Culture=neutral, PublicKeyToken=null]].

I'm not sure what's happening here (it looks like we're dealing with a List<> of enums?), but I knew I didn't need author, or link, so I removed the variable, property, and initialization in AbstractFeatureType (which is a superclass of DocumentType).

When I run the code again, I get this error (PROBLEM 3):

Unhandled Exception: System.InvalidOperationException: There was an error generating the XML document. --->
System.InvalidOperationException: The type Kml2_2.DocumentType was not expected.
Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

After digging around, I find out that KmlType.Item (which is an AbstractFeatureType) is missing an XmlElementAttribute:

[System.Xml.Serialization.XmlElementAttribute( "Document", typeof( DocumentType ) )]

So I prepend that to the Item property.  When I run the code again, it finally works, but now the XML looks like this (PROBLEM 4):

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.opengis.net/kml/2.2">
	<Document>
		<AddressDetails xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
			<PostalServiceElements>
				<EndorsementLineCode/>
				<KeyLineCode/>
				<Barcode/>
				<SortingCode/>
				<AddressLatitude/>
				<AddressLatitudeDirection/>
				<AddressLongitude/>
				<AddressLongitudeDirection/>
			</PostalServiceElements>
		</AddressDetails>
		<Region>
			<LatLonAltBox>
				<altitudeMode>clampToGround</altitudeMode>
			</LatLonAltBox>
			<Lod/>
		</Region>
	</Document>
</kml>

Which isn't that great, because I don't need my elements to come with default instances of everything.

So what I surmise is that 1 and 2 might either be a bug in the .xsd, I didn't generate the schema correctly, or there's a bug in the generator, 3 looks like a definite bug, and 4 looks like it should be an option in the schema generation (like a lazy initialization, as suggested by another poster).  Also, would it be possible to have, instead of "item" as the property name, to include the abstract class in the name or list, such as "AbstractFeatureTypeItems"?

Thanks for your consideration!

(Edits to make post a little more readable)

 

Jul 21, 2009 at 8:33 PM

I had a similar problem with my xsd. Tried generating using the standard xsd.exe and experienced similar problems.

You should look at the types stated in the errors and try to comment out XmlElementAttribute annotations above the Items property to isolate the problem type. Once you found it, you probably have to make some manual changes due to the source xsd using duplicate names...

Jul 9, 2012 at 7:59 PM

If you are using a standardised XSD (like for musicxml), really isn't an option...