XmlSerializer stopped working after updates

元气小坏坏 提交于 2019-12-04 17:25:52
public static XmlSerializer GetEntityXmlSerializer<TEntity>()
         where TEntity : Entity
{ 
    XmlAttributes ignoreAttribute = new XmlAttributes()
                                    {
                                         XmlIgnore = true,
                                    };

    // use base class of Entity, 
    // if you use type of implementation 
    // you will get the error.
    Type entityType = typeof(Entity);

    var xmlAttributeOverrides = new XmlAttributeOverrides();
    xmlAttributeOverrides.Add(entityType, "EntityConflict", ignoreAttribute);
    xmlAttributeOverrides.Add(entityType, "EntityState", ignoreAttribute);

    return new XmlSerializer(typeof(TEntity), xmlAttributeOverrides);
}

I am not sure why this would be happening, RIA Services entities are not XmlSerializable objects and the entities themselves are not decorated with the [Serializable] attribute. Have you added partial classes on the client side which decorate the entities with [Serializable] or modified the code generation in some way?

I got around this problem by using intermediary serializable POCO objects which were copies of my custom objects (which were inherited from Entity). The POCO objects did not inherit from Entity. I just updated their values from the original Entity objects. They then serialized quite nicely. Of course, when you de-serialize you need to update your Entity objects from the POCO objects.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!