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

大兔子大兔子 提交于 2019-12-17 18:56:50

问题


I have an 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

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

<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.

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.

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


回答1:


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.

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




回答2:


References

According to MSDN MinOccurs Attribute Binding Support, there are only 2 ways to get 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.

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

string.Empty

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

C#

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

VB

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

Solution

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

More Bad News

Even if it were possible, Nick DeVore linked to John Sounder's response in 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.




回答3:


According to the answers to this SO question, it isn't possible. John Saunders says:

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.



来源:https://stackoverflow.com/questions/3961112/how-to-make-a-dotnet-webservice-set-minoccurs-1-on-a-string-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!