Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

c# 4.0 - The XML element named 'name' from namespace references distinct types

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...