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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With