如何使dotnet webservice在字符串值上设置minOccurs ="1"

Sim*_*ers 8 .net xsd wsdl web-services asmx

我有一个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>
Run Code Online (Sandbox Code Playgroud)

我已经转换成使用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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的webservice中返回一个AA对象,它在服务描述中生成这个片段

<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> 
Run Code Online (Sandbox Code Playgroud)

从minOccrus ="1"中的XSD到的minOccurs ="0"上的自动生成的WSDL的变化引起的悲伤到机器上的系统的另一端.

当然,我可以提供编辑WSDL为他们用一只手,但我想自动生成一个适合他们的需要.

如何说服的dotnet输出的minOccurs ="1",在其自动生成的WSDL字符串类型还没有加入的nillable ="真"有什么建议?

小智 9

参考

根据MSDN MinOccurs属性绑定支持,只有两种方法可以获得MinOccurs = 1.

  1. 没有默认值或附带布尔字段的值类型.

    结果: output元素的minOccurs值设置为1

  2. 具有XmlElement属性的IsNullable属性设置为true的引用类型.

    结果: output元素的minOccurs值设置为1.在元素中,nillable属性也设置为true.

string类型的属性(不幸的是)总是具有默认值

string.Empty
Run Code Online (Sandbox Code Playgroud)

所以它不能有一个空的默认值.这意味着我们无法满足第一个解决方案.为字符串生成MinOccurs = 1的唯一方法是使元素可以为:

C#

[XmlElementAttribute(IsNullable = true)]
public string Item { ... }
Run Code Online (Sandbox Code Playgroud)

VB

<XmlElement(IsNullable:=True)>
Public Item As String
Run Code Online (Sandbox Code Playgroud)

唯一真正的解决方案是手动编辑XSD ... boo xsd.exe.

更多坏消息

即使有可能,Nick DeVore 在另一个线程中与John Sounder的响应相关联,该线程声明该字段不用于传入的XML.因此,用户仍然可以发送无效的XML.


Chr*_*isG 6

我注意到以下几行:

为了将XML Schema复杂类型与非XML特定的类绑定,.NET Framework不提供与minOccurs或maxOccurs属性等效的直接编程语言.

从这里:http://msdn.microsoft.com/en-us/library/zds0b35c(v = vs.85).aspx

  • ...有点糟透了:( (6认同)