none
WCF - Modify XML output RRS feed

  • Question

  • I need to add an outer tag in the xml output...

    I am returing an object based on a class.

    [DataContract]
    public class Car
    {

      [DataMember]
      public string Model {get; set;}

      [DataMember]
      public string Color {get; set;}

    }

    So I have the WCF returning...

    <Car>
     <Model>F150</Model>
     <Color>Red</Color>
    </Car>

    I simply want to modify the xml being returned by the service to included the outer tag Output...


    <Output>
    <Car>
     <Model>F150</Model>
     <Color>Red</Color>
    </Car>
    </Output>

    How might I accomplish this ?

    Thursday, December 4, 2014 2:42 PM

Answers

  • [Datacontract] public class Output { private car = new Car();

    [Datamember] public Car TheCar { get { return car; } set { car = value; } }

    Of course you would do some renaming here to get tags the way you wanted when Output object is XML serialized by WCF with Car in the parent object.

    Note: Parent and child object must be marked as Datacontracts with Datamembers.

    Thursday, December 4, 2014 6:41 PM

All replies

  • Then I would say don't use a class, make an XML Document with the tags and data you want and send it as string. Then you convert string back into an XML document on the client side and addrsss the XML Document object.
    Thursday, December 4, 2014 4:55 PM
  • Thanks for the response...

    I was wondering if there was some way to wrap the Car Class in a Class called Output, but I have not figured out how...



    Thursday, December 4, 2014 5:00 PM
  • [Datacontract] public class Output { private car = new Car();

    [Datamember] public Car TheCar { get { return car; } set { car = value; } }

    Of course you would do some renaming here to get tags the way you wanted when Output object is XML serialized by WCF with Car in the parent object.

    Note: Parent and child object must be marked as Datacontracts with Datamembers.

    Thursday, December 4, 2014 6:41 PM
  • Yes Thanks... I had just figured that out a few minutes ago !

    Thanks for your help !

    Thursday, December 4, 2014 6:44 PM