XmlSerializer - There was an error reflecting type

后端 未结 18 1528
借酒劲吻你
借酒劲吻你 2020-11-28 02:46

Using C# .NET 2.0, I have a composite data class that does have the [Serializable] attribute on it. I am creating an XMLSerializer class and passi

相关标签:
18条回答
  • 2020-11-28 03:18

    Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.

    You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore] attribute.

    XmlSerializer does not use the [Serializable] attribute, so I doubt that is the problem.

    0 讨论(0)
  • 2020-11-28 03:19

    I had the same issue and in my case the object had a ReadOnlyCollection. A collection must implement Add method to be serializable.

    0 讨论(0)
  • 2020-11-28 03:20

    I just got the same error and discovered that a property of type IEnumerable<SomeClass> was the problem. It appears that IEnumerable cannot be serialized directly.

    Instead, one could use List<SomeClass>.

    0 讨论(0)
  • 2020-11-28 03:25

    I had a similar problem, and it turned out that the serializer could not distinguish between 2 classes I had with the same name (one was a subclass of the other). The inner exception looked like this:

    'Types BaseNamespace.Class1' and 'BaseNamespace.SubNamespace.Class1' both use the XML type name, 'Class1', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.

    Where BaseNamespace.SubNamespace.Class1 is a subclass of BaseNamespace.Class1.

    What I needed to do was add an attribute to one of the classes (I added to the base class):

    [XmlType("BaseNamespace.Class1")]
    

    Note: If you have more layers of classes you need to add an attribute to them as well.

    0 讨论(0)
  • 2020-11-28 03:29
    [System.Xml.Serialization.XmlElementAttribute("strFieldName", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    

    Or

    [XmlIgnore]
    string [] strFielsName {get;set;}
    
    0 讨论(0)
  • 2020-11-28 03:31

    Also be aware that XmlSerializer cannot serialize abstract properties.. See my question here (which I have added the solution code to)..

    XML Serialization and Inherited Types

    0 讨论(0)
提交回复
热议问题