System.Xml.XmlDocument not able to go across the wire.

System.Xml.XmlDocument not able to go across the wire.

Old forum URL: forums.lhotka.net/forums/t/7723.aspx


ludwigs3rd posted on Friday, October 02, 2009

I'm using a wcf data portal like in PTracker wcfhost (no web service). I got this message:
Unhandled Exception: System.Runtime.Serialization.InvalidDataContractException: Type 'System.Xml.XmlDocument' cannot be serialized. Consider marking it with the DataContr
actAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for oth
er supported types.

Server stack trace:
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.XmlObjectSerializerContext.GetDataContract(Int32 id, RuntimeTypeHandle typeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.GetDataContract(Int32 id, RuntimeTypeHandle typeHandle)
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiTyp
e, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
etc...

this is my code:
private static readonly PropertyInfo _resultsProperty = RegisterProperty(p => p.Results);
public System.Xml.XmlDocument Results
{
get { return GetProperty(_resultsProperty); }
set
{
OnPropertyChanging("Results");
SetProperty(_resultsProperty, value);
OnPropertyChanged("Results");
}
}

Any help or advice is appreciated. Thanks!

tmg4340 replied on Friday, October 02, 2009

XmlDocument is not a serializable type, so you aren't going to get that to work in any remote-DP scenario.

You could get it to transmit if you changed it to a string value, and just sent the raw XML (using the XmlDocument.InnerXml property).  Of course, changing your property type may cause other issues.  If you're 100% sure you're always going to use WCF, you could create a second property (probably private) that exposes the string representation of your XML.  Then you can manually decorate your BO and all your properties with the appropriate WCF attributes, marking the string property with [DataMember] and not marking your XmlDocument property.  Then WCF shouldn't try to serialize the XmlDocument property.  But you'd want to test out how that will work with managed properties, since your "set" block would have to translate the string value back into an XmlDocument for your other property to work correctly.

HTH

- Scott

Copyright (c) Marimer LLC