XmlSerialization of derived classes with same name (Full Version)

All Forums >> [Ask XML questions here] >> .NET programming discussions



Message


hde -> XmlSerialization of derived classes with same name (2 October 2007)

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();
        }

      }
}




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.046875