7
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type ProfileChulbul was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterProfileDefinitionExportHolder.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)

if you see 'ProfileChulbul' is an object, I am trying to serialize

1
  • 1
    BreakHead, the question is still incredibly vague. Please post code for the class you are trying to serialize. Feb 7, 2011 at 17:01

2 Answers 2

25

This happens when the type you're serializing has a property of a type that is not statically known to the serializer instance. For example, if the type ProfileChulbul has a base type, which is how it is referenced from what you're serializing, the serializer won't know how to work with it.

You have a couple options for resolving this problem:

  • Add the [XmlInclude(typeof(ProfileChulbul))] attribute (and additional attributes for any other types that will be used) to ProfileChulbul's base class

  • Modify the class you use for serialization to use generics instead of Object

  • Pass typeof(ProfileChulbul) (and any other types that will be used) into the serializer constructor at runtime, like so:

    var knownTypes = new Type[] { typeof(ProfileChulbul), typeof(ProfileSomethingElse) };

    var serializer = new XmlSerializer(typeof(TheSerializableType), knownTypes);

1
  • 1
    Thanx Daniel, Stack Overflow rocks :)
    – BreakHead
    Feb 8, 2011 at 12:26
1

Based on the part of the stacktrace "Use the XmlInclude or SoapInclude attribute to specify types that are not known statically", I would bet you are trying to serialize an interface or collection of interfaces? Xml Serialization does not allow this, try marking the interface with the XmlInclude attributes.

4
  • So how should I do this, because the object I am getting to serialize is a generic object.
    – BreakHead
    Feb 7, 2011 at 16:50
  • 1
    BreakHead, that's exactly what's causing the error then. Can you use generics instead of a plain old Object? Feb 7, 2011 at 16:57
  • +1 i had the same experience :-( now i am using sharpserializer that can handle xml-(de-)serialisations.
    – k3b
    Feb 7, 2011 at 17:57
  • @BreakHead, if you serialize an object, you must know the concrete types that are to be serialized. Can you post some code to make it a bit clearer on what's going on? Feb 7, 2011 at 19:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.