XmlSerialier reflection error ??
-
Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class:
string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...;
I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following linexmlSer = new XmlSerializer(typeof(Booking[]));
As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it. -
Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class:
string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...;
I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following linexmlSer = new XmlSerializer(typeof(Booking[]));
As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.
/* This field will be ignored when serialized--
unless it's overridden. */
[XmlIgnoreAttribute]
public string Comment;- Eitsop What we do not understand we do not possess. - Goethe.
-
When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.
/* This field will be ignored when serialized--
unless it's overridden. */
[XmlIgnoreAttribute]
public string Comment;- Eitsop What we do not understand we do not possess. - Goethe.
What I have on my "public MyButton myBut;" is an [XmlIgnore] Could it be that the XmlSerializer have trouble serializing objects like Button,Label classes because they have to do something with Windows? Or perhaps that my object class is inheriting the Button object? From one confused guy..
-
What I have on my "public MyButton myBut;" is an [XmlIgnore] Could it be that the XmlSerializer have trouble serializing objects like Button,Label classes because they have to do something with Windows? Or perhaps that my object class is inheriting the Button object? From one confused guy..
I have tried out this code. This ignores the button altogether and seems to work just fine:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;namespace SerialTest
{
// This is the class that will be serialized.
public class Group
{
[XmlIgnoreAttribute]
public System.Windows.Forms.Button myButton;// a group name or something public string GroupName; // a comment of some sort... public string Comment; public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = new XmlSerializer(typeof(Group)); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Serialize the object and close the TextWriter. xSer.Serialize(writer, this); writer.Close(); }
}
}To run this the example I used was a form with a button and the following code on the
OnClick
event:Group a = new Group();
a.Comment="A sample comment";
a.GroupName="A sample group name";
a.myButton = this.button1;
a.SerializeObject("c:\\test.xml");That worked.
- Eitsop What we do not understand we do not possess. - Goethe.
-
I have tried out this code. This ignores the button altogether and seems to work just fine:
using System;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;namespace SerialTest
{
// This is the class that will be serialized.
public class Group
{
[XmlIgnoreAttribute]
public System.Windows.Forms.Button myButton;// a group name or something public string GroupName; // a comment of some sort... public string Comment; public void SerializeObject(string filename) { // Create an XmlSerializer instance. XmlSerializer xSer = new XmlSerializer(typeof(Group)); // Writing the file requires a TextWriter. TextWriter writer = new StreamWriter(filename); // Serialize the object and close the TextWriter. xSer.Serialize(writer, this); writer.Close(); }
}
}To run this the example I used was a form with a button and the following code on the
OnClick
event:Group a = new Group();
a.Comment="A sample comment";
a.GroupName="A sample group name";
a.myButton = this.button1;
a.SerializeObject("c:\\test.xml");That worked.
- Eitsop What we do not understand we do not possess. - Goethe.
Yeah this works I know. I think(hope i did) i mentioned it that mybutton is an object of
class MyButton:System.Windows.Forms.Button
What you have created is a "standard" button and i have a custom button that inherits System.Windows.Forms.Button. So i wonder if there is any problems with inherited classes using XmlSerializer ? -
Hi! Basically i want to serialize an array of objects Booking. That class has public fields of basic types like string,int. A very simple class containg information of the booking. I have managed to serialize/deserilize the array before but when i added a public field object of type 'MyButton' which inherits the System.Windows.Forms.Button class i got Reflection error of type Booking[] ? I tried using an [XmlIgnore] on the object not to serializr this but it doesn't work. Booking class:
string name; ...; ...; public Booking(string name,...,...) { this.name = name; ...; ...; myButton = new MyButton(); } ...;
I have a booking manager class that takes care of every booking and also instantiate the xmlSerializer object with the following linexmlSer = new XmlSerializer(typeof(Booking[]));
As I said, it worked fine until I added the MyButton object to the Booking class! Why can't I add an object to my class with the property of XmlIgnore on it and serialize that object ? Can't get a grip of what reflection error really means too! Thankful for suggestion on how to solve this. I want to use this XmlSerializer cause it makes the job a lot easier but if i cant workaround this problem I may have to build an xmldocument by hand and save it and i try to avoid it.Well, I do not immediately see any problem :(, but why is that button declared as public anyway?
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
-
When you say XmlIgnore, do you mean that you have applied the XmlIgnoreAttribute? i.e.
/* This field will be ignored when serialized--
unless it's overridden. */
[XmlIgnoreAttribute]
public string Comment;- Eitsop What we do not understand we do not possess. - Goethe.