I have a class like this:
public class Item
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("Id")]
public int Id { get; set; }
[XmlAttribute("Entry")]
public int Entry { get { return this.Id; } set { this.Id = value; } }
}
And I don't want to serialize the Entry
attribute but still want to be able to deserialize it from files that have said attribute.
If I either set XmlIgnore
it will not read during deserialization and if I don't it will write both Entry
and Id
to my serialized file which I don't want.
I know I could generate a secondary class with excludes Entry altogether and use that specific one for serializing, but I grew curious if there is a way to make so it will not serialize the Id or it will deserialize the Entry
into the Id
attribute instead?
Just to further clarify the issue, how the XML's are formatted is not something I control and what I am doing is merely a 3rd party application that will read those files and save them back. To me the Entry
attribute is redundant and unneeded hence why I save it to the Id as they are the same, however there are many elements with no Id
and instead they have the Entry
, once my application is used to read and re-save the file it removes the Entry
and saves it as Id
instead.
Also to serialize and deserialize it I have to inject the root element name:
XmlSerializer serializer = new XmlSerializer(typeof(T), new XmlRootAttribute(root));
using (XmlReader reader = XmlReader.Create(file))
{
return (T)serializer.Deserialize(reader);
}
Because the root name will differ from file to file I have to inject it.
No comments:
Post a Comment