C# XML serialization without defining types
Is there any way to build a generic method for XML serialization and
deserialization that doesn't require a bunch of gymnastics including
pre-defining types using something like XmlInclude? The code that I need
to build cannot rely on pre-defined types. I'm assuming Reflection could
be used here, but I can't find an adequate solution. The ones I've tried
from NuGet each have limitations:
Polenter seems to produce unusable output that cannot be deserialized
Global, Netfx requires type definitions ahead of time
Bender is beautiful but doesn't seem to support objects embedded within
the object being serialized
XSerializer and JsonFX are what I need on the serialization side but the
output won't deserialize
The standard XmlSerializer won't work for obvious reasons (pre-defined
types, XmlInclude, etc).
A simple example of what I need to serialize and then deserialize:
public class c1
{
public string name { get; set; }
public object obj { get; set; }
}
public class c2
{
public string city { get; set; }
}
public static void Main(string[] args)
{
c1 class_1 = new c1();
c2 class_2 = new c2();
class_1.name = "david";
class_2.city = "chicago";
class_1.obj = class_2;
string xml = <insert here>;
Console.WriteLine("XML: " + xml);
c1 deserialized = new c1();
deserialized = <insert here>;
Console.WriteLine("City: " + ((c2)c1.obj).city);
}
Does something like this even exist?
No comments:
Post a Comment