1

scratching my head on WCF ... I've got XML messages where the children of <DataFields> could be anything, e.g. ...

<Test1Root> 
  <CaseNo></CaseNo>
  <Activity></Activity>
  <DataFields>
     <AccountRef></AccountRef>
     <PropRef></PropRef>
     <User></User>
  </DataFields>
</Test1Root>

I've handled this in BizTalk using the <xs:any> for the <DataFields> ...

<xs:element name="DataFields">
  <xs:complexType>
    <xs:sequence>
      <xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

But I'm struggling to see how to handle this in a WCF [DataContract] ...

I tried using both svcutil.exe and xsd.exe to create the classes, and they both give the main elements (CaseNo, Activity, etc.) but neither seems to handle the xs:any of DataFields ...

  • svcutil has DataFields as an XmlElement
  • xsd has it as a class, but with an Any property of type XmlElement []

Is it possible to get a better handling of the child elements ?

SteveC
  • 15,808
  • 23
  • 102
  • 173

3 Answers3

2

Well, xs:any can be anything, so the best the .NET tools could do is give you an array of objects....

Since it can be anything, there's really not much you can do about it, right? It could be anything... so you need to use a type that could be anything.

If you really need that xs:any in your XML schema, and can't replace by e.g. a set of more specific xs:element (possibly inheriting from one another), I don't see how you could get better support...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks for the response, I was sort of expecting it, but had a (faint) hope there was a really neat way to handle this – SteveC Jul 05 '10 at 06:50
0

I had some problems with an old web service with the WSDL defining the response property using an "any" element.

Solved the problem with a custom message formatter. Wrote a post about it describing the solution in more detail http://devdump.wordpress.com/2012/10/26/working-with-legacy-web-services-and-wcf/, hope it saves others time as this was the only discussion I could find about this subject.

  • 3
    It would be better to summarize the contents of the link to make this a canonical answer that will not break when the link does. – mnel Oct 25 '12 at 22:37
  • 1
    @mnel Why not edit the answer yourself rather than complaining? – Carl Onager Aug 06 '13 at 08:03
0

You can do this. I have done it many times in a service. I haven't done WCF services much, but I believe they work the same way. The <xs:any> in the schema gets converted to XmlElement in the code. In your case an array of XmlElement.

SteveC
  • 15,808
  • 23
  • 102
  • 173
nick
  • 1
  • 1
    Looks like my '' got processed from above. It was supposed to read: '' in the schema gets converted to XmlElement in the code. – nick Jun 08 '11 at 15:25
  • Err, sorry ... I'm not getting your point ?? I mentioned that XSD.EXE generates an XmlElement[], so what are you saying ? – SteveC Jun 09 '11 at 08:34