I have a problem with implementing an IsDirty
mechanism with my XmlSerializer
system.
This is how my serialization is called:
public OCLMEditorModel()
{
DeSerialize();
}
public void Serialize()
{
XmlSerializer x = new XmlSerializer(_ModelData.GetType());
using (StreamWriter writer = new StreamWriter(_strPathModelDataXml))
{
x.Serialize(writer, _ModelData);
}
}
public void DeSerialize()
{
_ModelData = new OCLMModelData();
XmlSerializer x = new XmlSerializer(_ModelData.GetType());
using (StreamReader reader = new StreamReader(_strPathModelDataXml))
{
_ModelData = (OCLMModelData)x.Deserialize(reader);
}
}
It reads and saves perfectly, no issues there. But it is the IsDirty
flag I have issues with. Directly after the DeSerialize call ...
Ass the IsDirty are set to true. Even though all we have done is read it in from the computer. Example properties:
public class MaterialItem
{
[XmlAttribute]
public string Setting
{
get { return _Setting; }
set
{
_Setting = value;
MarkDirty();
}
}
private string _Setting;
[XmlText]
public string Material
{
get { return _Material; }
set
{
_Material = value;
MarkDirty();
}
}
private string _Material;
[XmlIgnore]
public bool IsDirty { get { return _isDirty; } }
private bool _isDirty;
public void MarkClean()
{
_isDirty = false;
}
protected void MarkDirty()
{
_isDirty = true;
}
public MaterialItem()
{
MarkClean();
}
}
Ideally, the flag should be false when we have just read it using XMLSerializer.
What am I doing wrong?
Thank you.