Customizing Web Service XML Communication RRS feed

  • Question

  • Hi there,

    I want to develop a Web Service communicating with a custom protocol based on XML and REST. Basically, i want to be able to define how the objects are converted into XML.

    Is this possible using WCF?



    Friday, April 30, 2010 2:09 PM


All replies

  • You can use XMLSerialiser to your contracts which have more control over your classes on serializing and deserializing, then the datacontract serializer, In addition to that you have messagecontracts where in you specfiy the header and body parts of the messages,

    So to controls header and body, use the messagecontracts , to control the contracts/classes to convert the properties into xml attribute / xml element, use the xml serialiser, here the example

     public class Person
      public int Age { get; set; }
      public string Name { get; set; }
     public class Employee
      public string category { get; set; }
      public Person Details { get; set; }

    Please mark as answer, if it solves your problem, Vote if it helps your problem
    • Marked as answer by Mog Liang Friday, May 7, 2010 10:19 AM
    • Unmarked as answer by Luís Cruz Monday, May 10, 2010 2:10 PM
    Friday, April 30, 2010 4:47 PM
  • The answer was quite confusing...


    From what i understood from webservices, the communication message is returned in a special XML format; we have to redefine that XML format to attend to a specific protocol. 

    It's something like this:

    <feed xmlns:sdata="http://schemas.sage.com/sdata/2008/1"
     <!-- Atom feed elements -->
     <title>Sage App | Sales Orders</title>
     <!-- Feed level links ... -->
     <!-- Categories ... -->
     <!-- Paging information ... -->
     <!-- Simple List Extensions ... -->
     <!-- Atom entry -->
     <title>Sales Order 43660</title>
     <link rel="self" type="application/atom+xml; type=entry" title="self"
     ('43660')" />
     <category scheme="http://schemas.sage.com/sdata/categories"
     term="resource" label="Resource"/>
     <content>Sales order 43660: amount 1553,1035</content>
     <!-- Resource payload -->
     <salesOrder sdata:key="43660" xmlns="http://schemas.sage.com/demoErp">
     <shipDate xsi:nil="true" />
     <orderLines sdata:url="https://interop.sage.com/sdata/accounts50/SDO/
     <!-- ... 8 more entries ... -->

    Best regards,

    Luís Cruz


    Monday, May 10, 2010 2:24 PM
  • Hi Luis,

    As other member suggested, WCF can use XmlSerializer for serilize .NET object into XML content(when tranfered over wire). Also, by default WCF use DatacontractSerializer, which doesn't have full control over object ---XML convertion, so you can manually swtich your service to use XML serializer.

    #Using the XmlSerializer Class

    #XmlSerializer vs DataContractSerializer: Serialization in Wcf

    Also, as you mentioned that the target service is REST based( seems it is a RSS like service from the sample XML you provided). Thus, you can also consider define your WCF service/client as REST based(use webhttpBinding) which can send or receive data as raw binary stream. And you can read the stream in any arbitrary format(binary or xml or text ) as you like. Here are the MSDN reference which mentioned this:

    #How to: Create a Service That Accepts Arbitrary Data using the WCF REST Programming Model

    #How to: Create a Service That Returns Arbitrary Data Using The WCF Web HTTP Programming


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, May 17, 2010 3:49 AM