0

I have a .NET 4 WCF web service which takes a data contract with an optional enumerable of strings. For example:

Service Code:

[DataMember(IsRequired = true)]
public string Something {
    get { return _Something; }
    set { _Something= value; }
}
private string _Something;

[DataMember(IsRequired = false)]
public string[] MoreThings {
    get { return _MoreThings.ToArray<string>(); }
    set { _MoreThings  = new List<string>(value); }
}
private List<string> _MoreThings = new List<string>();

WSDL:

  <xsd:complexType name="MyDataContract">
    <xsd:sequence>
      <xsd:element minOccurs="1" name="Something" type="xsd:string" /> 
      <xsd:element minOccurs="0" name="MoreThings" nillable="true" type="q1:ArrayOfstring" xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> 
    </xsd:sequence>
  </xsd:complexType>

I'm trying to interface with the service from a number of different platforms, but one is Perl using SOAP::WSDL (and wsdl2perl.pl in particular), which seems to be failing on recognizing "ArrayOfstring". Is there anything I can do in my code so that the WSDL would read like this:

  <xsd:complexType name="MyDataContract">
    <xsd:sequence>
      <xsd:element minOccurs="1" name="Something" type="xsd:string" /> 
      <xsd:element minOccurs="0" maxOccurs="unbounded" name="MoreThings" nillable="true" type="xsd:string" /> 
    </xsd:sequence>
  </xsd:complexType>

I'm open to other suggestions, as well. Basically, I'm trying to do something similar to declaring a method MyOperation(string something, params string[] moreThings), where any number of additional moreThings parameters may be passed, but in a way that doesn't cause interop issues with non-.NET platforms.

Paul Hooper
  • 839
  • 2
  • 7
  • 15
  • Does this answer your question? [XML validation with XSD: how to avoid caring about the sequence of the elements?](https://stackoverflow.com/questions/3325247/xml-validation-with-xsd-how-to-avoid-caring-about-the-sequence-of-the-elements) – Michael Freidgeim Jan 19 '21 at 01:15

1 Answers1

3

I think you cannot achieve that with DataContractSerializer - you must switch to XmlSerializer. You can simply take your schema you need and let tools (run VS command prompt) generate class for you.

  • For DataContractSerizlizer use: svcutil.exe /dataContractsOnly File.xsd but as I mentioned before I don't think that DataContract can represent your schema
  • For XmlSerializer use: xsd.exe /classes File.xsd

Once you have the class file simply add it to your service project and use it in your service's operations. In case of XmlSerializer you must mark either whole service contract / implementation or operation contracts / implementations using types generated by XSD.exe with XmlSerializerFormat attribute.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thumbs up for pointing me to xsd.exe, I hadn't used that before. Unfortunately, the end result was getting the same WSDL. After some more digging, I found that the real problem was that my ArrayOfstring was defined as another ComplexType *below* my data contract, and if I moved it *above* my data contract, SOAP::WSDL did just fine with it. But then it bombed on other things, prompting me to vow to never use SOAP::WSDL again. – Paul Hooper Jun 29 '11 at 22:12