命名空间中名为“name”的XML元素引用不同类型 [英] The XML element named 'name' from namespace references distinct types

查看:272
本文介绍了命名空间中名为“name”的XML元素引用不同类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙。我在从服务器反序列化数据时出现错误,

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


顶部的XML元素'Name'from namespace'
Object1.LocalStrings和System.String。使用XML属性指定
一个或多个元素的另一个XML名称或命名空间。

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.

属性名称和列表< SupportedIp> 。 SupportedIp类也包含属性Name。请参阅下面的代码:

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

当应用程序到达 XmlSerializer 部分,错误显示。我看到有些相关的职位,但没有具体的答案。

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

推荐答案

从你写的我认为问题是,具有两种不同类型的内容(字符串类型和地址字符串类型)的相同元素名称(namespace =,name =Name),这在xml中是非法的。这意味着每个xml解析器应该引发你打印的致命错误。解决方案是更改元素的名称或使用相同的名称,但将它们与不同的命名空间相关联。例如代替:

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 = "")]

您可以输入:

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

核心问题似乎是XMLSerializer使用XSD模式验证。这意味着您定义的每个XmlElementAttribute都具有附加的类型(阅读更多从此处)。 XSD约束之一是元素声明一致约束,意味着具有相同名称(和命名空间)的任何两个元素必须具有相同的类型(从此处)。

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).

这篇关于命名空间中名为“name”的XML元素引用不同类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆