Forum main page
Most
active member
Login page
Register
Log out
Search page
View our event calendar
Add general event such as an
upcoming software release or conference
View all birthdays
Public image gallery
Upload your images in your profile
Profile editing page
Subscription list page
Address book page
Member list page
View the most
active member
User groups listing
Private message page
|
|
Users viewing this topic: none
|
|
Login | |
|
XmlSerialization of derived classes with same name - 2 October 2007
|
|
|
hde
Posts: 1
Score: 0 Joined: 2 October 2007 Status: offline
|
Hi, I'm running into an error when using the XmlSerializer ..... : Types 'NamespaceA.NameBaseClass' and 'NamespaceB.NameBaseClass' both use the XML type name, 'NameBaseClass', from namespace 'XmlNamespaceB'. Use XML attributes to specify a unique XML name and/or namespace for the type. The cause of the error is obvious : 2 classes with the same name (one inherits from the other one, but in different .Net namespaces) are repsonsible for the error. A simplified code example will explain this furthermore ... (see below) I can think of 2 solutions to handle this issue, but unfortunately they don't really fit in the "real-life" context. Solution 1 :Define an appropriate Xml-Namespace by means of the XmlTypeAttribute for each class. [XmlType(Namespace = "NamespaceA")]But : the NamespaceA belongs to a third party component. So, no way to intervene in this assembly. Solution 2 :Rename the derived class (in namespaceB) to an unique name within namespaceA and namespaceB.But : the classes in the namespaceB are intensively used within our company. About 90% of our classes inherit from this class (= the one in namespaceB).So, refactoring this class would have a huge impact on all our applications.... Solution 3 :Any new idea would be great ..... :0) using Systemusing System.Collections.Generic;using System.Text;using System.Xml.Serialization;using System.IO;using System.Xml; namespace NamespaceA //This is the thirdparty assembly{ //[XmlType(Namespace="XmlNamespaceA")] public class NameBaseClass { private string _firstName; public string FirstName { get { return _firstName; } set { _firstName = value; } } } } namespace NamespaceB //This is the company wide used assembly { [XmlType(Namespace = "XmlNamespaceB")] public class NameBaseClass : NamespaceA.NameBaseClass { private string _lastName; public string LastName { get { return _lastName; } set { _lastName = value; } } } } namespace MyTest { public class MyNameClass : NamespaceB.NameBaseClass //This is my concrete class used in my development project. { } public class TestSameNameClass //The test { public void TestSerialization() { MyNameClass name = new MyNameClass(); name.FirstName = "yves"; name.LastName = "leterme"; //Here we have the error : // // Types 'NamespaceA.NameBaseClass' and 'NamespaceB.NameBaseClass' both use the XML type name, 'NameBaseClass', from namespace 'XmlNamespaceB'. Use XML attributes to specify a unique XML name and/or namespace for the type. // XmlSerializer serializer = new XmlSerializer(typeof(MyNameClass)); StreamWriter writer = new StreamWriter("C:\\Temp\\TestName.xml"); serializer.Serialize(writer, name); writer.Close(); } } }
|
|
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|