Reputation: 1581
I am not sure if that can be done, so I'll say that before I start.
I have an XML file which contains the usual stuff (strings, bools etc), but I also want it to have as one of the nodes the EventHandler for the specific click event. All click events are the normal object s, EventArgs e variety.
My holding class looks like this
namespace testxmlui
{
[Serializable]
[XmlRoot("xmlformat")]
public class XmlFormatData
{
private List<xmlformdata> xmlform;
public XmlFormatData()
{
xmlform = new List<xmlformdata>();
}
[XmlElement("Element")]
public xmlformdata[] Forms
{
get { return xmlform.ToArray(); }
set { xmlform = new List<xmlformdata>(value); }
}
}
[Serializable]
public class xmlformdata
{
public xmlformdata()
{
}
public string buttonName
{ get; set; }
public int buttonAction
{ get; set; }
public int buttonEvent
{ get; set; }
public bool HasEventAttached
{ get; set; }
public EventHandler EventHandle
{ get; set; }
}
}
this is then deserialized using
private void GenerateUI()
{
XmlFormatData f;
f = null;
try
{
XmlSerializer s = new XmlSerializer(typeof(XmlFormatData));
TextReader r = new StreamReader(pathToUse);
f = (XmlFormatData)s.Deserialize(r);
r.Close();
}
catch (System.IO.FileNotFoundException e)
{
Console.WriteLine("Error : {0}", e.Message);
}
catch (System.InvalidOperationException s)
{
Console.WriteLine("Invalid Operation Error : {0}, {1}", s.Message, s.StackTrace);
}
// there is more, but it's all UI code, so not really a problem
}
Do I need to do something special to include the EventHandler and can anyone suggest why I'm getting a deserialization error?
The deserialization throwback reads
Invalid Operation Error : There was an error reflecting type 'testxmlui.XmlFormatData'., at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00000] in :0 at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00000] in :0 at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00000] in :0 at System.Xml.Serialization.XmlSerializer..ctor (System.Type type) [0x00000] in :0 at testxmlui.MainActivity.GenerateUI () [0x00004] in /Volumes/Developer/Developer/ftrack2/testxmlui/testxmlui/MainActivity.cs:62
Thanks
Upvotes: 0
Views: 853
Reputation: 1063433
XmlSerializer is a data serializer - it does not and cannot serialize delegates / events, as delegates are fundamentally about implementation - not data. Additionally, serializing events means you need to cascade the serializer into many more types that are outside the expected model. So no, that isn't really viable with that serializer. IMO serializing events usually suggests a code-smell anyway - again, they are not the data.
Re the exception: look at the InnerException - it actually gives very detailed messages, but you need to dig down a level.
Upvotes: 2