【发布时间】:2010-01-13 19:11:04
【问题描述】:
我有一个 XSD,并使用 xsd.exe 工具创建 c# 类。在 Web 服务中,我在 MessageContract 中接受这些创建的对象之一的实例。
这个问题的xsd相关部分在这里:
<xs:element name="Tasks">
<xs:complexType>
<xs:sequence>
<xs:element ref="Task" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Task"> ... </xs:element>
xsd 创建了这个:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Tasks {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Task")]
public Task[] Task;
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Task { ... }
SOAPUI 从 WSDL 创建了一个如下所示的 soap 请求:
<Tasks>
<Task>
<Task>
.. task data here
</Task>
</Task>
</Tasks>
注意额外的包装元素。尝试运行该soap 请求时,我收到反序列化错误: 行x 位置y 中的错误: 来自命名空间'ZZZ' 的'Element' 'WWW' 不是预期的。期望元素“SSS”
在生成的 SOAP 请求中找到无关节点后,我使我的新请求如下所示:
<Tasks>
<Task>
...task data here
</Task>
</Task>
现在反序列化器“工作”了,但在我的方法中,Tasks 对象包含一个空的 Task 数组。
所以我的问题是:为什么自动请求生成器会创建包装任务对象,为什么当我删除它时,我的任务对象中会出现一个空数组?
【问题讨论】:
标签: c# wcf xsd xml-serialization serialization