xsc*_*ape 9 xml namespaces xml-serialization c#-4.0
请帮忙.从服务器反序列化数据时遇到错误,
来自命名空间''的顶级XML元素'Name'引用不同类型Object1.LocalStrings和System.String.使用XML属性为元素指定另一个XML名称或命名空间.
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
当应用程序到达XmlSerializer部分时,将显示错误.我看过一些相关的帖子,但没有一个具体的答案.
Mir*_*ea 12
从你写的内容我认为问题是你有(namespace="", name="Name")两个不同类型的内容(字符串类型和localstrings类型)相同的元素名称,这在xml中是非法的.这意味着每个xml解析器都应该引发您打印的致命错误.解决方案是更改元素的名称或使用相同的名称,但将它们与不同的名称空间相关联.例如,而不是:
[XmlElementAttribute(Namespace = "")]
Run Code Online (Sandbox Code Playgroud)
你可以把:
[XmlElementAttribute(Namespace = "http://test.com/2010/test")]
Run Code Online (Sandbox Code Playgroud)
核心问题似乎是XMLSerializer使用XSD架构验证.这意味着您定义的每个XmlElementAttribute都附加了一个类型(从这里开始阅读更多).其中一个XSD约束是"元素声明一致"约束,这意味着具有相同名称(和命名空间)的任何两个元素必须具有相同的类型(从此处阅读更多内容).
希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
10725 次 |
| 最近记录: |