XmlSerializer ignores class attributes. I'm writing simple serializer, and I used [Serializable]
and [NonSerialized]
attributes, also I tried to use [XmlRoot]
and [XmlIgnore]
. And I've noticed, although the field has the attribute [NonSerialized]
it is serialized.
And it also ignores other attributes such as [XmAtribute]
. Then I've noticed that it's even not necessary to use any attributes, and I can serialize class without these attributes, how can I ignore some fields?
My class:
[Serializable]
public class Route
{
int busNumber;
string busType, destination;
DateTime departure, arrival;
[NonSerialized]DateTime creationDate;
...
}
And I'm trying to serialize List<Route>
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Stream stream = File.OpenWrite(Environment.CurrentDirectory + "\\routes.xml");
XmlSerializer xmlSer = new XmlSerializer(typeof(List<Route>));
xmlSer.Serialize(stream, ((FileForm)ActiveMdiChild).routes);
stream.Close();
}
I believe that you are looking for the XmlIgnoreAttribute. Also, properties that need to be serialized must be declared as
public
.Usage as follows: