locked
sgen.exe error: The top XML element from namespace '' references distinct types

    السؤال

  • Dear All,

    I am programming in C#.Net using the .Net 3.5 framework running Visual Studio 2008.

    I have two classes that have the same name but are in different .net and xml namespace.

    When I run sgen.exe on the dll, I get the ""Error: The top XML element 'Person' from namespace '' references distinct types Service1.Person and Service2.Person. Use XML attributes to specify another XML name or namespace for the element or types."

    Below is the sample code:

    Service1.cs

    ========

    using System;

    namespace Service1
    {
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "ns1")]
        public partial class Person
        {
            public string name;
        }
    }

     

    Service2.cs

    =======

    using System;

    namespace Service2
    {
        [System.Xml.Serialization.XmlTypeAttribute(Namespace = "ns2")]
        public partial class Person
        {
            public string name;
        }
    }

     

    I have the same Person xml element, but they are in different xml and .net namespaces, so should not sgen be able to create separate serializer for the two Person classes? Is there a solution to this problem without resorting to using different typename.

    I noticed that the xmlserializer dll that gets generated creates all the serializer classes under Microsoft.Xml.Serialization.GeneratedAssembly namespace.

    Should it not create serializer classes utilizing the xml namespace, like Microsoft.Xml.Serialization.GeneratedAssembly.ns1

     

    Many thanks in advance for your help.

    Ashok

     

    06/جمادى الثانية/1431 07:29 ص

الإجابات

  • Hi Ashok,

    XmlTypeAttribute doesn't actually mean that your CLR types have different namespaces. Instead, you want to use XmlRootAttribute to distinguish the two like this:

    using System;

    namespace Service1
    {
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "ns1")]
        public partial class Person
        {
            public string name;
        }
    }

    namespace Service2
    {
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "ns2")]
        public partial class Person
        {
            public string name;
        }
    }

    MSDN has more about the distinction between the two attributes: http://msdn.microsoft.com/en-us/library/2baksw0z(VS.80).aspx

    Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute

    There are two attributes that can be applied to a class (and only a class): XmlRootAttribute and XmlTypeAttribute. These attributes are very similar. The XmlRootAttribute can be applied to only one class: the class that, when serialized, represents the XML document's opening and closing element—in other words, the root element. The XmlTypeAttribute, on the other hand, can be applied to any class, including the root class.

    For example, in the previous examples, the <tt>Group</tt> class is the root class, and all its public fields and properties become the XML elements found in the XML document. Therefore, there can be only one root class. By applying the XmlRootAttribute, you can control the XML stream generated by the XmlSerializer. For example, you can change the element name and namespace.

    The XmlTypeAttribute allows you to control the schema of the generated XML. This capability is useful when you need to publish the schema through an XML Web service. The following example applies both the XmlTypeAttribute and the XmlRootAttribute to the same class.

    24/جمادى الثانية/1431 10:09 م
    المشرف