Michael Westwort
Michael Westwort

Reputation: 924

Read minOccurs and default values from a WSDL file

I am coding a client for a web service. In the web service definition file, "minOccurs" and default values are defined. How can I access these minOccurs and defaults?

In the WSDL file, the element is defined like this:

<xs:element minOccurs="0" name="optionalEntry" type="tns:optionalValueType" default="NULL"/> where optionalValueType is an enumeration.

The web service is referenced as webservice and the value NULL can be set manually (like any other possible value) as webservice.optionalValueType.NULL.

Upvotes: 4

Views: 1685

Answers (1)

johnnyjob
johnnyjob

Reputation: 390

Oversimplifying, WSDL describes an XML schema for documents which can be consumed by a web service endpoint. The default attribute is part of XML Schema standard, and the standard tells that value specified in this attribute should be automatically assigned to the element when no other value is specified (https://www.w3schools.com/xml/schema_simple.asp).

There are some tools which can generate client side code from a given WSDL document. You didn't specify which tool you used, so I assume it is WSDL.exe (part of Visual Studio), but there are others (e.g. SoapUI). So, answer to your question depends on the tool you use.

Strictly speaking, the tools are not obliged to provide an API to expose value of default attribute. They should only generate code which will behave like defined in the standard, namely, if value is not specified, default value should be used. Example:

// Account property is defined like this:
// <s:element minOccurs="0" maxOccurs="1" name="account" type="s:string" default="FOO" />
var connInfo = new ConnectionInfo();
Console.WriteLine(connInfo.account); // Will print "FOO".

Going back to your question, I can see the following ways to get default value from the generated client code:

  1. Create an instance of a class, then read the fields. The fields will be initialized to their default values (like in the code snippet above).
  2. WSDL.exe adds DefaultValueAttribute to every property which has default value. Example (generated code):
public partial class ConnectionInfo 
{
    private string accountField;

    public ConnectionInfo() 
    {
        this.accountField = "FOO";
    }

    [System.ComponentModel.DefaultValueAttribute("FOO")]
    public string account 
    {
        get 
        {
            return this.accountField;
        }
        set 
        {
            this.accountField = value;
        }
    }
}

So it should be possible to get the value from that attribute, using some reflection:

var type = typeof(ConnectionInfo);
var prop = type.GetProperty("account");
var attr = (DefaultValueAttribute)prop.GetCustomAttributes(
    typeof(DefaultValueAttribute), true).First();
Console.WriteLine(attr.Value); // Will print "FOO".

As of minOccurs attribute, I cannot see a way to get it, other than reading the WSDL schema yourself.

Upvotes: 4

Related Questions