views:

43

answers:

1

Is it possible to make LinqToSql use XmlSerializer to deserialize a typed XML field to an C# object?

Currently I have the following code: (note that I had to use XElement.ToString() to use XmlSerializer)

    protected static T Deserialize<T>(string xmlString) where T : class
    {
        var xmlSerializer = new XmlSerializer(typeof(T));
        var obj = xmlSerializer.Deserialize(new StringReader(xmlString));
        return (T)obj;
    }
    //...
    {
       return ATable.Select(x => Deserializer<AContentType>(x.Content.ToString()));
       // x.Content is an XElement
       // AContentType is a class that can be used by XmlSerializer to deserialize 
       //    The class was generated using xsd.exe
    }

If I can't make LinqToSql use XmlSerializer, can I at least make it return a string instead of XElement?

BTW. Any thoughts how to improve the Deserialize function are welcome.

A: 

No, I don't think you can - you can return a XML field from SQL Server as a string and then do any manual processing you want on it - including parsing it using Linq-to-XML.

But I am not aware of any built-in functionality for Linq-to-SQL to deserialize a XML field into an object - how would it even do that if there's no XML schema present in SQL Server? You can have both typed and untyped XML fields (without any schema restrictions) in SQL Server....

Marc

marc_s