views:

237

answers:

1

I am working on an Audit Log for an application that uses Linq-To-Sql. I want to serialise an object to stores its values in a XML column in a SQL Server databse.

My problem is that when I try to serialize a Linq-To-Sql obejct that it attempts to serialize all associated entities and entity sets.

My first attempt at a solution was to create seperate class tos tore what I need to serialize and then pass this to an XmlSerializer.

I suppose what I really want to know is how to get the XmlSerializer to ignore certain types of properties - E.g entities and entity sets.

Would I have to write my own XmlSerializer?

All and any advice would be aprreciated.

+1  A: 

You need to use the DataContractSerializer, but this will still serialize a partial object graph.

The better solution is to generate your own Linq2SQL classes and apply the XmlIgnore/DataMember attributes (depending on whether you use XmlSerializer or DataContractSerializer).

leppie
+1 Yes, use the DataContractSerializer... sample: http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/73e82932-d41c-4e51-a91a-704aead00526
KristoferA - Huagati.com
Be careful that `DataContractSerializer` doesn't serialize fields that are part of the implementation of the objects. It does so for Entity Framework types in .NET 3.5, and I think I saw it do the same for L2S.
John Saunders