Sunday, October 2, 2011

The type was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

Post from: http://www.johnsoer.com/blog/?p=125&cpage=1#comment-5494

  1. public class Test
  2. {
  3. private string _Field = "";
  4. public string Field
  5. {
  6. get { return _Field; }
  7. set { _Field = value; }
  8. }
  9. }
  10. public class TestInherited : Test
  11. {
  12. }
  13. public class Container
  14. {
  15. private Test ivField;
  16. public Test Field
  17. {
  18. get { return _Field; }
  19. set { _Field = value; }
  20. }
  21. }
  22. Container _Test = new Container();
  23. _Test.Field = new TestInherited();
Solution:
new XmlSerializer(typeof(Container), new Type[] { typeof(TestInherited) });


This solution was not working for me because:

There was a namspace on my XmlRoot element [XmlRoot("Container", Namespace = "http://namespace", IsNullable = false)]

When i removed namespase, the problem was gone.

Hope this helps someone.

No comments: