XML Deserialization with GeoFramework structs

Aug 6, 2010 at 10:00 AM
Hello I have come across an InvalidOperationException with message "Use the Latitude(XmlReader) constructor to create a new instance instead of calling ReadXml." and so on when I tried to deserialize GeoFramework objects. Looking into the codes I found that the ReadXml() for IXmlSerializable interface was implemented to throw this exception. However, by using the following code, I can't enforce the serializer to call the constructor as the exception message tried to instruct: XmlSerializer serializer = new XmlSerializer(typeof(Latitude)); Latitude lat = (Latitude)serializer.Deserialize(stream); I probably too new to C# to know how I could instruct the XmlSerializer to stop calling ReadXml() and create the instance using new Latitude(XmlReader) instead. I understand that the GeoFramework classes are implemented for thread-safety and try not to expose the accessor methods to avoid updates to the struct contents. But could anyone shred me some lights how I can deserialize my structs back please? Pongthai
Aug 9, 2010 at 1:37 PM
This issue has been fixed, but there hasn't been a new release that includes it yet. Try downloading the latest change set from the source code page and see if that works for you.
Aug 10, 2010 at 9:13 AM
Thanks a lot BigstickCarpet for your reply. I'll try the newest load and let you know if it works.
Aug 10, 2010 at 11:28 AM
I tested the latest source, it looks like the ReadXml() for all immutable objects (Position, and so on) do not advance to the end of the surrounding tag before they return. Hence I was unable to deserialize a list of those objects. I tried by adding reader.ReadEndElement() at the end of ReadXml() method for those immutable classes and it works quite ok. Do you happen to encounter this problem when deserialize an xml?
Aug 10, 2010 at 2:07 PM
Hmmm.... you're definitely correct that most of the types are not calling ReadEndElement. Oddly enough though, I've never had any problems serializing these objects. I think it may depend on which serializer is being used. In my case, I'm usually exposing the GeoFramework types over a WCF web service, so they're being serialized by the WCF data contract serializer rather than the normal .Net XmlSerializer. Nonetheless, this is clearly a bug. The types should serialize and deserialize correctly, regardless of which serializer is used. Can you please submit a new issue for this? I can't make any promises as to when I'll get it resolved, but as long as there's an issue for it, it won't get forgotten.
Aug 11, 2010 at 2:32 AM
Yep, that must be the cause. I used XmlSerializer to serialize the types to an xml file. The WriteXml() normally positions the reader beyond the open tag of the containing tag, but the ReadXml() gives you a reader that is positioned just before the open tag of the containing tag. I'm not familiar with the WCF but it looks like the specification of IXmlSerializable stated this way too (http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.readxml.aspx). May be you probably need to handle both cases? --- By the way, thanks a lot for your excellent code and great support. They really did save my life!!