简体   繁体   中英

XML serialization related problem with XmlIgnore

When serializing a class I get an error if the XmlIgnore attribute is commented. When I uncomment the XmlIgnore attribute it works fine. Can anyone tell me why a property can't be serializable?

Normally, it will tell you why very clearly in the exception - look in particular at the InnerException; however, to summarise:

For a custom type SomeType to be serializable via XmlSerializer (either as the root object or via a property, ie public SomeType MemberName {get;set;} )

  • it must be public
  • it must have a public parameterless constructor
  • a few generic combinations may not be supported
  • it must be expected (exposing data as object is a no-go, for example; subtypes of SomeType must be advertised in advance, typically via [XmlInclude(...)] )
  • it must be concrete (non- abstract ), or have concrete implementations defined via [XmlInclude(...)]

Using [XmlIgnore] removes the type from consideration, so types that are not xml-friendly can be avoided. You can also try IXmlSerializable for those cases

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM