11

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Simon Withers
  • 173
  • 1
  • 2
  • 10
  • 2
    in case you stumble here hoping for the opposite (`minOccurs=0`) see http://social.msdn.microsoft.com/Forums/en-US/30d3517b-98c5-44d0-b621-4f3343ce8ea2/getting-wsdl-to-produce-minoccurs0 -- you add an ignored property `[XmlIgnore]public bool {{TheProperty}}Specified { get; set; }` and set it when 'TheProperty' is set. – drzaus Nov 05 '13 at 17:43

3 Answers3

12

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.

Community
  • 1
  • 1
Rapps
  • 142
  • 1
  • 6
  • Don't you need the IsNullable property set? What do you get with it set to true, or to false? – John Saunders Jun 06 '13 at 20:16
  • In my case, the more bad news wasn't an issue, as I wasn't trying to rely on the framework to validate that part of the XML, just trying to prevent the humans looking at the WSDL from consistently pestering me with "why is your minOccurs wrong" questions. – Simon Withers Jun 08 '13 at 22:29
  • Simon - I'm concerned about the end user seeing the wrong information as well. My only fully automated solution is to post-process the xsd with a batch file or exe to correct the minOccurs values. I have my own attribute attached to the properties in my class so an exe can key off of that. @JohnSaunders - I'm not sure if you're asking to clarify my response or if you are proposing a solution. The 'IsNullable' term within the property attribute solves the 'minOccurs' issue, but causes another by adding 'isNullable'; which Simon points out at the end of the original question. – Rapps Jun 10 '13 at 16:07
8

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

ChrisG
  • 1,316
  • 1
  • 11
  • 14
1

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.

Community
  • 1
  • 1
Nick DeVore
  • 9,748
  • 3
  • 39
  • 41
  • I'm not actually trying to use the WSDL to validate input, I'm trying to say that may output is guaranteed to contain the string. – Simon Withers Nov 04 '10 at 16:38