Search code examples
wcfxml-serializationdatacontractserializer

DataContractSerializer: preserve string member that happens to be raw xml?


I'm a little inexperienced with the DataContract paradigm, and I'm running into a deserialization problem. I have a field that's a string, but it contains xml and it's not being deserialized correctly. I have a feeling that it's because the DCS is treating it as input to the serializer and not as an opaque string object.

Is there some way to mark a DataMember in code to say "This thing is a string, don't treat its contents as xml" similar to XmlIgnore?

Thanks!


Solution

  • Turns out the easiest way to do this was just to cast the xml field coming from sql server to a varchar(max) when retrieving it from the database.

     CAST(CONVERT(XML,[RawXml],0) AS VARCHAR(MAX)) AS RawXml
    

    In this case, the serializer seems to be ignoring it as desired. Thanks for the help though!