Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

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

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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...