I am trying to XML serialize / deserialize an object in C #. The catch is that this object is of a type that was not declared in the same assembly as the code causing the serialization. Instead, it comes from an assembly loaded dynamically at runtime, ant, so at compile time it is not known the code that causes serialization.
The type I'm trying to serialize is as follows:
//Assembly = P.dll namespace EDFPlugin.Plugin1 { [Serializable] [XmlRoot(Namespace = "EDFPlugin.Plugin1")] [XmlInclude(typeof(Options))] public class Options { private string _username; private string _password; public string Username { get { return _username; } set { _username = value;} } public string Password { get { return _password; } set { _password = value; } } } }
As I mentioned earlier, the code that I use to try to serialize / deserialize this object is in an assembly that is not aware of the Options type at compile time (since it dynamically loads P.dll at run time). However, I managed to serialize the type correctly using this code:
basically, as you can see, by calling GetType() , I can get around the problem of the lack of knowledge like Options at compile time, everything works fine.
The problem occurs when trying to deserialize:
Since I donβt know the predefined type, I basically canβt properly configure the XmlSerializer . Using a generic object , as shown in the code above, throws an exception:
"<Options xmlns='EDFPlugin.Plugin1'> was not expected."
How can i solve this?
source share