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

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

问题描述

请帮忙.从服务器反序列化数据时出错,

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

来自命名空间 '' 的顶部 XML 元素 'Name' 引用了不同的类型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.

我有一个 ObjectType 类,它包含属性 Name 和 List.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") 有两种不同类型的内容(string 类型和 localstrings 类型),在 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天全站免登陆