i have abstract class couple of concrete implementations. needs serializing xml in order send system - working fine. however, need able deserialize same xml structure back. no matter try, don't seem able this. class structure below:
abstract class:
[xmlincludeattribute(typeof(concretefooone))] [xmlincludeattribute(typeof(concretefootwo))] [xmlincludeattribute(typeof(concretefoothree))] [xmlroot(elementname = "foodata", namespace="http://foo.bar")] public abstract partial class abstractfoo { // abstract props etc. } concrete class example:
public partial class concretefooone : abstractfoo { // properties, constructor etc. } xml root example:
<foodata xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:type="concretefooone" requestresponse="request" xmlns="http://foo.bar"> only included xml root example appears issue is. can serialize fine, on deserialization, if deserialize passing in abstract type, of course exception stating type "abstractfoo" abstract. changed logic instead concrete type (concretefooone in case) passed serializer. "http://foo.bar'> not expected". presuming because serializer doesn't know root node should be?
i have root node defined on abstract class same all concrete implementations. concrete type defined "requestresponse" attribute (or xsi:type attribute can work if present gives actual type name). there way make serializer pick required off abstract class or going wrong way this?
- note can't change class structure based closely off xml schemas provided 3rd party.
thanks in advance anyone's this, appreciated.
add [xmlroot(elementname = "foodata", namespace = "http://foo.bar")] sub-classes
here example made:
[xmlincludeattribute(typeof(concretefooone))] [xmlincludeattribute(typeof(concretefootwo))] [xmlincludeattribute(typeof(concretefoothree))] [xmlroot(elementname = "foodata", namespace = "http://foo.bar")] public abstract partial class abstractfoo { // abstract props etc. } [xmlroot(elementname = "foodata", namespace = "http://foo.bar")] public class concretefooone : abstractfoo { public int myprop { get; set; } } [xmlroot(elementname = "foodata", namespace = "http://foo.bar")] public class concretefootwo : abstractfoo { } [xmlroot(elementname = "foodata", namespace = "http://foo.bar")] public class concretefoothree : abstractfoo { } class program { static void main(string[] args) { var serializer = new system.xml.serialization.xmlserializer(typeof(abstractfoo)); using (var stream = new filestream("test.txt", filemode.openorcreate)) { serializer.serialize(stream, new concretefooone() { myprop = 10 }); stream.flush(); } using (var stream = new filestream("test.txt", filemode.openorcreate)) { var c = serializer.deserialize(stream); } } }
Comments
Post a Comment