【发布时间】:2010-10-18 16:19:34
【问题描述】:
我有一个 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://a.com/a.xsd"
targetNamespace="http://a.com/a.xsd"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我使用 XSD.exe v2.0.50727.3615 将其转换为 C# 类,生成代码如下
namespace A {
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://a.com/a.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://a.com/a.xsd", IsNullable=false)]
public partial class A {
private string itemField;
/// <remarks/>
public string Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
}
我在我的 web 服务中返回了一个 A.A 对象,它在服务描述中生成了这个 sn-p
<s:schema elementFormDefault="qualified" targetNamespace="http://a.com/a.xsd">
<s:element name="Test2Result">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Item" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
从 XSD 中的 minOccrus="1" 到自动生成的 WSDL 中的 minOccurs="0" 的变化正在给系统另一端的机器带来麻烦。
我当然可以提供手动编辑的 WSDL 供他们使用,但我希望自动生成的 WSDL 能够满足他们的需求。
关于如何说服 dotnet 在其自动生成的 WSDL 中为字符串类型输出 minOccurs="1" 而不添加 nillable="true" 的任何建议?
【问题讨论】:
-
如果你在这里偶然发现相反的情况 (
minOccurs=0),请参阅 social.msdn.microsoft.com/Forums/en-US/… -- 你添加一个被忽略的属性[XmlIgnore]public bool {{TheProperty}}Specified { get; set; }并在设置 'TheProperty' 时设置它。
标签: .net web-services xsd wsdl asmx