How to write in a Xml using derived classes?

Dystortion

New Member
maybe the title is not the best description.My situation is, depending on a interface I want to write one of its properties which is object. Please watch the following example:\[code\]public interface IFoo{ // anothers properties... object Value { get; }}\[/code\]And I have three implementations of it here, check that Value can be is hidden the true datatype.\[code\]public class FooA : IFoo{ public string Value { get; set; } object IFoo.Value { get { return Value; } }}public class FooB : IFoo{ public int Value { get; set; } object IFoo.Value { get { return Value; } }}public class FooC : IFoo{ public List<double> Value { get; set; } object IFoo.Value { get { return Value; } }}\[/code\]In my scenario was very important not using generics. So with this implementation I need to write in a Xml just the property Value.For example:\[code\] static void Main(string[] args) { List<IFoo> fooList = new List<IFoo>() { new FooA() { Value = "http://stackoverflow.com/questions/10407422/String" }, new FooB() { Value = http://stackoverflow.com/questions/10407422/2 }, new FooC() { Value = new List<double>() {2, 3.4 } } }; // Write in a Xml the elements of this list with the property Value (and the problem is the datartype, // strings, ints, lists, etc) // ... }\[/code\]I was thinking using XmlSerialization, but I don't want to store all the properties of the object, just Value property.I need a way to write the property Value (not to store all the properties of that interface) and a Way to know which implementation of IFoo I'm using.UPDATE: I wanna have an XML file like this.\[code\]<Foos> <Foo xsi:Type="FooA" Value="http://stackoverflow.com/questions/10407422/String" /> <Foo xsi:Type="FooB" Value="http://stackoverflow.com/questions/10407422/2" /> <Foo xsi:Type="FooC" > <Value> <List xsi:Type="Double"> <Element>2</Element> <Element>3.4</Element> </List> </Value> </Foo</Foos>\[/code\]This is just an idea. The idea is to store just a property of IFoo and the capability to get the concrete object with the value.UPDATE:Thanks to Raj Nagalingam to help me with the idea. This is what I've accomplished.\[code\]public interface IFoo{ object Value { get; }}public abstract class Foo<T> : IFoo, IXmlSerializable{ [XmlElement] public T Value { get; set; } [XmlIgnore] object IFoo.Value { get { return Value; } } XmlSchema IXmlSerializable.GetSchema() { return null; } void IXmlSerializable.ReadXml(XmlReader reader) { throw new NotImplementedException(); } void IXmlSerializable.WriteXml(XmlWriter writer) { XmlSerializer serial = new XmlSerializer(Value.GetType()); serial.Serialize(writer, Value); }}public class FooA : Foo<string> { }public class FooB : Foo<int> { }public class FooC : Foo<List<Double>> { }public class FooContainer : List<IFoo>, IXmlSerializable{ public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { throw new NotImplementedException(); } public void WriteXml(XmlWriter writer) { ForEach(x => { XmlSerializer serial = new XmlSerializer(x.GetType()); serial.Serialize(writer, x); }); }}class Program{ static void Main(string[] args) { FooContainer fooList = new FooContainer() { new FooA() { Value = "http://stackoverflow.com/questions/10407422/String" }, new FooB() { Value = http://stackoverflow.com/questions/10407422/2 }, new FooC() { Value = new List<double>() {2, 3.4 } } }; XmlSerializer serializer = new XmlSerializer(fooList.GetType(), new Type[] { typeof(FooA), typeof(FooB), typeof(FooC) }); System.IO.TextWriter textWriter = new System.IO.StreamWriter(@"C:\temp\demo.xml"); serializer.Serialize(textWriter, fooList); textWriter.Close(); }}\[/code\]And it works, but I guess it's impossible to recover the list with the concrete classes, Using the standard serialization I see that it's written using xsi, and this time I can't see them. How can I recover my objects?
 
Back
Top