0

I have set up my WCF service as follows:

<ServiceContract()>
Public Interface MyInterface

 <OperationContract()>
 Function Search(ByVal parm1As String, ByVal parm2 As String, ByVal parm3 As String) As MyResponse

End Interface

<DataContract()>
Public Class MyResponse

<DataMember()>
Public Property SearchResult() As SearchRes

<DataMember()>
Public Property RecordInfo() As List(Of RecordInf)

<DataContract()>
Public Class SearchRes
    <DataMember()>
    Public Property Prop1() As String

    <DataMember()>
    Public Property Prop2() As Integer

    <DataMember()>
    Public Property Prop3() As String
End Class

<DataContract()>
Public Class RecordInf
    <DataMember()>
    Public Property Prop4() As String

    <DataMember()>
    Public Property Prop5() As String

    <DataMember()>
    Public Property Prop6() As List(Of MyList)

    <DataMember()>
    Public Property Prop7() As String

    <DataMember()>
    Public Property Prop8() As String

    <DataMember()>
    Public Property Prop9() As String

    <DataMember()>
    Public Property Prop10() As String
End Class

<DataContract()>
Public Class Contr
    <DataMember()>
    Public Property Prop11() As String
End Class

The only problem with this is the response returns like this:

<s:Header />
  <s:Body>
    <SearchResponse xmlns="http://tempuri.org/">
      <SearchResult xmlns:a="http://schemas.datacontract.org/2004/07/MyService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:RecordInfo>
          <a:MyResponse.RecordInf>
            <a:Prop10>Yes</a:Prop10>
            <a:Prop6>
              <a:MyResponse.MyList>
                <a:Prop11>Blah</a:Prop11>
              </a:MyResponse.MyList>
            </a:Prop6>
            <a:Prop5>9780800720957</a:Prop5>
            <a:Prop8>pb</a:Prop8>
            <a:Prop9>9.45</a:Prop9>
            <a:Prop7>Blah</a:Prop7>
            <a:Prop4>Blah</a:Prop4>
          </a:MyResponse.RecordInf>
        </a:RecordInfo>
        <a:SearchResult>
          <a:Prop3 i:nil="true" />
          <a:Prop2>1</a:Prop2>
          <a:Prop1>1</a:Prop1>
        </a:SearchResult>
      </SearchResult>
    </SearchResponse>
  </s:Body>

As you can see from the above just after a:RecordInfo it puts the class - a:MyResponse.RecordInf which I don't want. How do I set this up correctly so the XML does not nest in the class name there?

5
  • I removed <DataContract()> above the class names but it didn't change anything in the XML output. Jun 20, 2014 at 17:24
  • So what is your desired xml output?
    – Styxxy
    Jun 20, 2014 at 20:01
  • I don't want the parts that have a:Myresponse before the class name. Jun 20, 2014 at 20:58
  • Did my answer help you in any way? Just wondering if you were able to solve your problem.
    – Styxxy
    Jun 22, 2014 at 13:41
  • Still trying to unravel your answer - can you see what I put below it please? Thanks! Jun 23, 2014 at 14:07

2 Answers 2

0

You have declared RecordInfo as a list of RecordInf objects, so potentialy there may be several objects in list, and WCF needs opening and closing tag to distinguish one record from another. Otherwise, all properties from all records would be under same tag.

If your RecordInfo always contains just one record - replace list with single RecordInf structure.

1
  • Yes there could be more than one record info. I just don't want the tags with the name of the class. Jun 20, 2014 at 20:58
0

This is the default (and normal) behaviour of the DataContractSerializer on properties of a collection type (which a List is). As per documentation (MSDN):

Customizing the Repeating Element Name in List Collections

List collections contain repeating entries. Normally, each repeating entry is represented as an element named according to the data contract name of the type contained in the collection.

In the CustomerList examples, the collections contained strings. The data contract name for the string primitive type is "string", so the repeating element was &lt;string&gt;.

You can also change this behaviour. You will have to change your DataContract a little however. You have to introduce your own list type (Inherits List(Of T)) and then you can annotate this new list type with a CollectionDataContract attribute, in which you can set the ItemName to control how the list is being serialized.

Following list class definition will do the trick for you. Just change the RecordInfo in your MyResponse class to the type RecordInfList.

<CollectionDataContract(ItemName:="RecordInf")>
Class RecordInfList
    Inherits List(Of RecordInf)
End Class

Please be aware, for following remark from the documentation (MSDN):

Customizing Collection Types

You can customize collection types by using the CollectionDataContractAttribute attribute, which has several uses.

Note that customizing collection types compromises collection interchangeability, so it is generally recommended to avoid applying this attribute whenever possible. For more information about this issue, see the "Advanced Collection Rules" section later in this topic.

6
  • Thanks for this but I have probably coded it wrong. I am getting this error: Unable to cast object of type 'System.Collections.Generic.List`1[MyService.MyResponse+RecordInf]' to type 'RecordInfList'. I changed to the following line of code: Public Property RecordInfo() As RecordInfList And added the CollectionDataContract code snippet you put up there. Jun 23, 2014 at 14:01
  • @user3715441 You have to change the initialisation of the property too. You probably have now somewhere in your code x.RecordInf = New List(Of RecordInf). Change it to x.RecordInfo = New RecordInfList() and you should be good to go.
    – Styxxy
    Jun 23, 2014 at 15:17
  • Thanks, that almost solved it! But now I am getting this: '<a:RecordInfo> <a:RecordInf>' So within RecordInfo will be a list of RecordInf. But I want just a list of RecordInfo. So one RecordInfo after another. Jun 23, 2014 at 15:30
  • This is entirely normal. <a:RecordInfo> is the property, and each <a:RecordInf> item is an element of the list (in that property).
    – Styxxy
    Jun 23, 2014 at 15:47
  • Yeah, that's not what they're expecting though. Not sure what they're expecting is possible... Jun 23, 2014 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.