gpt4 book ai didi

xml - namespace 中名为 'name' 的 XML 元素引用了不同的类型

转载 作者:数据小太阳 更新时间:2023-10-29 01:43:13 25 4
gpt4 key购买 nike

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

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> . SupportedIp 类也包含属性 Name。请引用我下面的代码:

[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 部分时,会显示错误。我看过一些相关的帖子,但没有具体的答案。

最佳答案

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

[XmlElementAttribute(Namespace = "")]

你可以放:

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

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

希望对您有所帮助。

关于xml - namespace 中名为 'name' 的 XML 元素引用了不同的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10532271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com