I have made a class which derives from the TreeNode class. I want some of the members from the base class to be serialized and some from the derived class. I'm unable to do this.
What I'm trying to do is something like this:
class Foo : TreeNode {
public string FooName;
public string FooID;
}
class Bar : TreeNode {
public string BarName;
}
Foo f=new Foo();
Bar b1=new Bar();
b1.BarName="b1";
Bar b2=new Bar();
b2.BarName="b2";
f.Nodes.Add(b1);
f.Nodes.Add(b2);
I would like to have it serialized to something like this:
<Foo>
<Bar BarName="b1"/>
<Bar BarName="b2"/>
</Foo>
So I want part of the base class (i.e. TreeNode.Nodes) to be serialized, but not all.
The problem I'm facing is that when I try to serialize/deserialize the derived class using XmlSerializer I get the following error: "There was an error reflecting property 'ContextMenu'." when I make a new XmlSerializer.
Thanks for any help.

Serializing derived classes in XML
Deepak Asani
I'm not absolutely certain, but I assume you're referring to a WinForms TreeNode, right I'm going to assume that for the rest of this comment.
In either case, I don't think you can XML-serialize it. TreeNode is binary serializable and implements ISerializable to override the default binary serialization behavior, but it doesn't look like that's what you're after - avoiding some potential default serialization problems in the process, but it does not implement IXmlSerializable and so attempts to create an XmlSerializer instance based on TreeNode (or subtypes) will use the default serialization which, as you pointed out, chokes on the ContextMenu - actually, that's not the only type in the hierarchy that would doom the serialization, as there is no custom XML Serialization attribute decoration to be found in the type. It just doesn't look like it was intended to be serialized to XML.
Do you want the XML to use in a configuration file or something If so, then you're probably going to have to roll your own export routine. In fact, it kind of looks like you'd be stuck doing that, anyway, if you only want part of your instance to serialize. Since you don't have source for the TreeNode type, you would be unable to control its XML serialization even if it was capable of doing it.
Are you actually using the TreeNode subclass(es) in a TreeView, or are you using TreeNode instances to logically group sets of objects for some other purpose If it's the latter, you'd be better off just implementing your own collections - that way you could control their serializability.
Best of luck,
Jared