hi,
1) I need to serialize a base and a subclass.
2) Base class has the property "AnEnum" of type enum in namespace "Namespace1"
namespace Namespace1 { using NS2=Namespace2; using System.Xml.Serialization; using System.Runtime.Serialization; using System.ComponentModel; [XmlType("Namespace1AnEnum")] public enum AnEnum { //[XmlEnum(Name = "Namespace1AnEnum")] TE1, TE1000, TEX, } [XmlInclude(typeof(NS2.SubLearnClass))] //[XmlRootAttribute("LearnClass", Namespace = "http://tempuri.org/Namespace1")] [XmlRootAttribute("LearnClass")] public class LearnClass { //[XmlAttributeAttribute(AttributeName = "APropertyNamespace1", Namespace = "http://tempuri.org/Namespace1")] //[XmlAttributeAttribute("APropertyOfNamespace1", Namespace = "http://tempuri.org/Namespace1")] [XmlAttributeAttribute("APropertyOfNamespace1")] public AnEnum AProperty { get; set; } } }
3) Subclass has the property "AnEnum" of type enum in namespace "Namespace2"
namespace Namespace2 { using Namespace1; using System.Xml.Serialization; [XmlType("Namespace2AnEnum")] public enum AnEnum { //[XmlEnum(Name = "Namespace2AnEnum")] TE2, TE2000, TE2X, } //[XmlRootAttribute("SubLearnClass", Namespace = "http://tempuri.org/Namespace2")] [XmlRootAttribute("SubLearnClass")] public class SubLearnClass : LearnClass { //[XmlAttributeAttribute(AttributeName = "APropertyNamespace2", Namespace = "http://tempuri.org/Namespace2")] //[XmlAttributeAttribute("APropertyOfNamespace2", Namespace = "http://tempuri.org/Namespace2")] [XmlAttributeAttribute("APropertyOfNamespace2")] public AnEnum AProperty { get; set; } } }
4)Serialization code is like:
namespace _CompisoControllerBoardApplication.Test { [TestClass] public class LearnClassTest { [TestMethod] public void WriteReadLearnClassTest() { LearnClass lClass = new NS2.SubLearnClass(); lClass.AProperty = AnEnum.TE1; try { IXmlService xmlService = new Xmlservice(); xmlService.WriteObjectToXML(lClass, @"..\..\aLearnClassTest.xml"); LearnClass lReadClass = xmlService.GetObjectFromXML<LearnClass>(@"..\..\aLearnClassTest.xml"); } catch (Exception ex) { } } } }
5) When i try to serialize i get the exception in German below, i write the one in Egnlish above:
Element SubLearnClass AProperty of type Namespace2.AnEnum hides out the base class elementLearnClass AProperty of type Namespace1.AnEnum. Use XmlElementAttribute or XmlAttributeAttribute to give a new name.
System.InvalidOperationException: Fehler beim Reflektieren der Eigenschaft 'AProperty'. ---> System.InvalidOperationException: Element SubLearnClass AProperty des Typs Namespace2.AnEnum blendet das Basisklassenelement LearnClass AProperty des Typs Namespace1.AnEnum aus. Verwenden Sie XmlElementAttribute oder XmlAttributeAttribute, um einen neuen Namen anzugeben.
6) How can i save the subclass object with its own AnEnum property?
Hint: if i use the type string instead of enum for the property AnEnum and set
[XmlAttributeAttribute("APropertyOfNamespace2")] to [XmlAttributeAttribute("APropertyOfNamespace1")]
, then it succeeds serializing in xml only APropertyOfNamespace1 = ..., but at least it does.
7)Please dont ask questions like why use same property names in subclasses etc... if u use XAML datbinding and you update yourdatamodels (dataobjects) quite often, then in the subclasses you may change the implementation, but use the same propertynames, so you dont have to change any other thing on the viewmodel side. But i need to serialize my datamodels for gods sake! :D
thanks,
Evren
what thought comes around, goes as code around...whatever