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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3961112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:49:30  来源:igfitidea点击:

How to make a dotnet webservice set minOccurs="1" on a string value

.netweb-servicesxsdwsdlasmx

提问by Simon Withers

I have an XSD:

我有一个 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>

Which I have converted into a C# class using XSD.exe v2.0.50727.3615 which generates code as follows

我已使用 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;
            }
        }
    }
}

I am returning an A.A object in my webservice, which produces this snippet in the service description

我在我的网络服务中返回一个 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> 

The change from minOccrus="1" in the XSD to minOccurs="0" on the auto-generated WSDL is causing grief to the machine on the other end of the system.

从 XSD 中的 minOccrus="1" 更改为自动生成的 WSDL 上的 minOccurs="0" 导致系统另一端的机器悲痛。

I could of course provide a hand edited WSDL for them to use, but I would like the auto-generated one to suit their needs.

我当然可以提供手动编辑的 WSDL 供他们使用,但我希望自动生成的 WSDL 能够满足他们的需求。

Any suggestions on how to convince dotnet to output minOccurs="1" for a string type in its autogenerated WSDLs without also adding nillable="true"?

关于如何说服 dotnet 在其自动生成的 WSDL 中为字符串类型输出 minOccurs="1" 而不添加 nillable="true" 的任何建议?

采纳答案by ChrisG

I note the following line:

我注意到以下行:

For binding XML Schema complex types with non-XML-specific classes, the .NET Framework does not provide a direct programming language equivalent to the minOccurs or maxOccurs attribute.

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

from here: http://msdn.microsoft.com/en-us/library/zds0b35c(v=vs.85).aspx

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

回答by Rapps

References

参考

According to MSDN MinOccurs Attribute Binding Support, there are only 2 ways to get MinOccurs = 1.

根据 MSDN MinOccurs Attribute Binding Support,只有两种方法可以获得MinOccurs = 1

  1. Value type with no default value or accompanying boolean field.

    Result:minOccurs value of output element is set to 1

  2. Reference type with an XmlElement attribute's IsNullable property set to true.

    Result:minOccurs value of output element is set to 1. In the element, the nillable attribute is also set to true.

  1. 没有默认值或伴随布尔字段的值类型。

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

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

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

A property of type string(unfortunately) always has a default value of

字符串类型的属性(不幸的是)总是有一个默认值

string.Empty

so it cannot ever have a nulldefault value. This means we can't ever satisfy the first solution. The only way to generate a MinOccurs=1for strings is to make the element nullable:

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

C#

C#

[XmlElementAttribute(IsNullable = true)]
public string Item { ... }

VB

VB

<XmlElement(IsNullable:=True)>
Public Item As String

Solution

解决方案

The only real solution is to edit the XSD manually... boo xsd.exe.

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

More Bad News

更多坏消息

Even if it were possible, Nick DeVore linked to John Sounder's responsein another thread which states that the field isn't used for incoming XMLs. So it's still possible for the user to send invalid XML.

即使有可能,Nick DeVore在另一个线程中链接到John Sounder 的响应,该线程指出该字段不用于传入的 XML。所以用户仍然有可能发送无效的 XML。

回答by Nick DeVore

According to the answers to thisSO question, it isn't possible. John Saunderssays:

根据thisSO question的答案,这是不可能的。 约翰桑德斯说:

It turns out that the WSDL is not used to validate incoming XML. It wouldn't matter whether or not you could specify minOccurs - it would not be used to validate the input.

事实证明,WSDL 不用于验证传入的 XML。您是否可以指定 minOccurs 无关紧要 - 它不会用于验证输入。