using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using System.Xml.Serialization;
public static void Main()
Console.WriteLine("Result of serializing SqlXml.Null: ");
Console.WriteLine(test.GetXml());
public static void Test()
Console.WriteLine("Initial {0} using AsGml():", dt2);
Console.WriteLine("------- Step 1: serialiization using datatable with SqlGeographyDTO------");
var xml=SerializeDataTable(dt2);
Assert.IsTrue(xml == SerializeDataTable(dt2));
Console.WriteLine("\nStep2: DeSerialiization the same xml using datatable with SqlGeographyDTO");
var dt3=DeSerializeDataTable(xml);
Console.WriteLine("Writing data from the generated datatable:");
Assert.IsTrue(xml == SerializeDataTable(dt3));
public static string SerializeDataTable(DataTable dt)
var writer = new StringWriter();
dt.WriteXml(writer,XmlWriteMode.WriteSchema );
var xml=writer.ToString();
public static DataTable DeSerializeDataTable(string xml)
DataTable dt=new DataTable();
StringReader srXMLtext = new StringReader(xml);
static DataTable GetGeoTable2()
DataTable dataTable = new DataTable("table");
dataTable.Columns.Add("f1", typeof(SqlGeographyDTO));
dataTable.Columns.Add("id", typeof(int));
DataRow newRow = dataTable.NewRow();
newRow["f1"] = (SqlGeographyDTO)SqlGeography.Point(20, 10, 4326);
dataTable.Rows.Add(newRow);
newRow = dataTable.NewRow();
newRow["f1"] = (SqlGeographyDTO)SqlGeography.STGeomFromText(new SqlChars("POINT(4 6)"), 4326);
dataTable.Rows.Add(newRow);
static void DumpDataTable(DataTable dataTable)
foreach (DataRow row in dataTable.Rows)
Console.WriteLine("{0} |f1: {1} ", ID, f1);
public class SqlGeographyDTO
const int DefaultSRID = 4326;
public int? STSrid { get; set; }
public XElement Geography { get; set; }
public static implicit operator SqlGeographyDTO(SqlGeography geography)
if (geography == null || geography.IsNull)
return new SqlGeographyDTO
STSrid = geography.STSrid.IsNull ? (int?)null : geography.STSrid.Value,
Geography = geography.AsGml().ToXElement(),
public static implicit operator SqlGeography(SqlGeographyDTO dto)
return SqlGeography.Null;
var sqlXml = dto.Geography.ToSqlXml();
var geography = SqlGeography.GeomFromGml(sqlXml, dto.STSrid.GetValueOrDefault(DefaultSRID));
public override string ToString()
return Geography == null ? "" : Geography.ToString(SaveOptions.DisableFormatting);
public static class XNodeExtensions
public static SqlXml ToSqlXml(this XNode node)
using (var reader = node.CreateReader())
return new SqlXml(reader);
public static class SqlXmlExtensions
public static XElement ToXElement(this SqlXml sql)
if (sql == null || sql.IsNull)
using (var reader = sql.CreateReader())
return XElement.Load(reader);
public class AssertionFailedException : System.Exception
public AssertionFailedException() : base() { }
public AssertionFailedException(string s) : base(s) { }
public static class Assert
public static void IsTrue(bool value)
public static void IsTrue(bool value, string message)
throw new AssertionFailedException(message ?? "failed");
public static class XmlSerializationHelper
public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
serial = serial ?? new XmlSerializer(typeof(T));
T returnValue = default(T);
using (StringReader reader = new StringReader(xmlString))
object result = serial.Deserialize(reader);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
return obj.GetXml(null, omitStandardNamespaces);
public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
ns = new XmlSerializerNamespaces();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();