I tried to serialize a DataTable object to xml. The DataTable has a column with type 'geography', which contains instances of type SqlGeography.
The following code is used to serialize the datatable to xml:
var writer = new StringWriter();
dt.WriteXmlSchema(writer); //get schema
//serialize to xml
dt.WriteXml(writer);
Console.WriteLine(writer.ToString());
The generated xml string is not complete with the loss of all Geo elements Lat,Long,...
It contains only STSrid element.
The following is the generated xml string:
<table>
<f1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<STSrid>4326</STSrid>
</f1>
<id>1</id>
</table>
<table>
<f1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<STSrid>4326</STSrid>
</f1>
<id>2</id>
</table>
This means that you can't use the xml for deserialization, and you loose the SqlGeography data.
The schema is generated correctly:
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="table" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="table">
<xs:complexType>
<xs:sequence>
<xs:element name="f1" msdata:DataType="Microsoft.SqlServer.Types.SqlGeography, Microsoft.SqlServer.Types, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" type="xs:anyType" minOccurs="0" />
<xs:element name="id" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
You can find the complete C# program online to show the problem.
My Question:
1) What I missed to get a valid xml serialization for SqlGeography column?
2) Is't a bug in the DataTable.WriteXml method to handle a complex object?