Reputation: 32400
MSDN gives a good example of how to make the XmlSerializer class ignore a property in a class to be serialized: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx
I'm using the XmlSerializer class to serialize an array of objects that inherit from the EntityObject class and I want XmlSerializer to ignore the EntityKey property. I've written the following code:
private void setupXmlSerializer()
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributesToOverride = new XmlAttributes();
attributesToOverride.XmlIgnore = true;
overrides.Add(typeof(MyEntityObject), "EntityKey", attributesToOverride);
completeXmlSerializer = new XmlSerializer(typeof(MyEntityObject[]), overrides);
}
However, when I run my code, I get the same result I was getting before I added the code to ignore EntityKey. What do I need to do to make XmlSerializer ignore EntityKey for each object in the array to be serialized?
Edit: In response to the answer I received, I tried writing the following code:
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributesToOverride = new XmlAttributes();
attributesToOverride.XmlIgnore = true;
attributesToOverride.XmlArrayItems.Add(new XmlArrayItemAttribute("EntityKey", typeof(MyEntityObject[])));
overrides.Add(typeof(MyEntityObject), attributesToOverride);
completeXmlSerializer = new XmlSerializer(typeof(MyEntityObject[]), overrides);
When I run this code, I get an exception: XmlRoot and XmlType attributes may not be specified for the type MyNamespace.MyEntityType
Edited Again: I've written the following code, which runs, but doesn't ignore the EntityKey. I'm wondering if this could have something to do with the way Entity Framework designs classes.
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributesToOverride = new XmlAttributes();
attributesToOverride.XmlArrayItems.Add(new XmlArrayItemAttribute("EntityKey", typeof(EntityType[])));
attributesToOverride.XmlIgnore = true;
overrides.Add(typeof(EntityType), "EntityKey", attributesToOverride);
completeXmlSerializer = new XmlSerializer(typeof(EntityType[]), overrides);
Edit: I don't know if this is relevant, but the EntityKey property is declared in the EntityObject abstract class with the following attributes:
[DataMember]
[Browsable(false)]
public EntityKey EntityKey { get; set; }
There is a second property called EntityState which is declared with XmlIgnore, so I haven't had a problem with having to ignore it.
[XmlIgnore]
[Browsable(false)]
public EntityState EntityState { get; }
Edit: I've made a discovery. If I try to ignore one of the actual data properties on the EntityObject, it works fine. I wonder if it could have something to do with either that EntityKey is declared with attributes or that it is a complex type while the other properties are primitive types.
Upvotes: 1
Views: 3934
Reputation: 1063298
Firstly, you need to attach the override to the member, not just the type - there is an Add
overload that accepts the member-name.
Secondly, it is EntityObject
that declares this member, and you need to attach the override to the declaring type.
Putting those together:
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attributesToOverride = new XmlAttributes();
attributesToOverride.XmlIgnore = true;
overrides.Add(typeof(EntityObject), "EntityKey", attributesToOverride);
var completeXmlSerializer = new XmlSerializer(
typeof(MyEntityObject[]), overrides);
Which then works.
Upvotes: 0
Reputation: 3526
You aren't actually specifying what sort of an attribute it is you want to override. Use something like
attributesToOverride.XmlArrayItems.Add(new XmlArrayItemAttribute("EntityKey", typeof(MyEntityObject[])));
overrides.Add(typeof(MyEntityObject), "EntityKey", attributesToOverride); // note "EntityKey" here
This is important because otherwise it thinks you want to ignore EntityKey on the array itself, but since it doesn't have it it won't ignore it.
--updated to show second line change
Upvotes: 0