13

Please help. I've got an error while deserializing the data from the server,

The top XML element 'Name' from namespace '' references distinct types Object1.LocalStrings and System.String. Use XML attributes to specify another XML name or namespace for the element or types.

I have a class ObjectType which contains properties Name and List<SupportedIp>. SupportedIp class contains property Name also. Please refer to my code below:

[XmlRootAttribute("SupportedIp", Namespace = "http://test.com/2010/test", IsNullable = false)]
public partial class SupportedIp
{[XmlElementAttribute(Namespace = "")]
    public string Name
    {
        get;
        set;
    } .... }


[GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[SerializableAttribute()]
[DebuggerStepThroughAttribute()]
[DesignerCategoryAttribute("code")]
[XmlTypeAttribute(Namespace = "http://test.com/2010/test")]
[XmlRootAttribute("ObjectType", Namespace = "http://test.com/2010/test", IsNullable = false)]
public partial class ObjectType
{

    /// <remarks/>
    [XmlElementAttribute(ElementName = "", Namespace = "")]
    public LocalStrings Name
    {
        get;
        set;
    }

    /// <remarks/>
    [XmlArrayAttribute(ElementName = "Supportedip", Namespace = "")]
    [XmlArrayItemAttribute(IsNullable = false, Namespace = "")]
    public List<Supportedip> Supportedip
    {
        get;
        set;
    }
}

When application reaches to XmlSerializer part, error displays. Ive seen somewhat related post but there's no concreate answer.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
xscape
  • 3,318
  • 9
  • 45
  • 86

3 Answers3

19

From what you wrote I think that the problem is that you have the same element name (namespace="", name="Name") with two different types of content (string type and localstrings type), which is illegal in xml. This means that every xml parser should raise the fatal error you have printed. The solution is to change either the name of the element or use the same name but associate them with different namespaces. For example instead of:

[XmlElementAttribute(Namespace = "")]

you could put:

[XmlElementAttribute(Namespace = "http://test.com/2010/test")]

The core problem seems to be that XMLSerializer uses XSD schema validation. Meaning that each XmlElementAttribute you define has a type attached (read more from here). One of the XSD constraints is "Element Declarations Consistent" constraint meaning that any two elements with the same name (and namespace) have to have the same type (read more from here).

Hope it helps.

Sergio
  • 6,900
  • 5
  • 31
  • 55
Mircea
  • 915
  • 6
  • 12
0

The argument you declare in the method header must be unique to all web methods in the namespace. As the argument is the top level xml tag of the soap:body. Good luck.

0

Read The Error: Use XML attributes to specify another XML name or namespace for the element

Example :

[XmlElement("Animal", typeof(Dog), Namespace = "...Dog")]
[XmlElement("Animal", typeof(Cat), Namespace = "...Cat")]
public Animal Animal;
Stav Bodik
  • 2,018
  • 3
  • 18
  • 25