Abhilash Saggam
Abhilash Saggam

Reputation: 11

How to get XML response through WCF Service Website?

I need the XML output using WCF Service website
interface IService class

public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "GET",
    ResponseFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "GetPay")]
    Payload GetPay();
}
[XmlRoot(ElementName = "payload")]
public class Payload
{
    [XmlElement(ElementName = "firstname")]
    public string Firstname { get; set; }
    [XmlElement(ElementName = "secondname")]
    public string Secondname { get; set; }
    [XmlElement(ElementName = "number")]
    public string Number { get; set; }
}
[XmlRoot(ElementName = "payloads")]
public class Payloads
{
    [XmlElement(ElementName = "payload")]
    public List<Payload> Payload { get; set; }
}

My Service Class is below

public class Service : IService
{
    public Payload GetPay()
    {
        return new Payload(); 
    }
}

My Web.congig file code

<?xml version="1.0"?>
    <configuration>
        <system.web>
              <compilation debug="true" targetFramework="4.0"/>
        </system.web>
        <system.serviceModel>
             <behaviors>
                 <serviceBehaviors>
                     <behavior>
                       <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                        <serviceMetadata httpGetEnabled="true"/>
                        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                        <serviceDebug includeExceptionDetailInFaults="false"/>
                   </behavior>
               </serviceBehaviors>
           </behaviors>
       <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
   </system.serviceModel>
   <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>

I need output in following format. Please help

<?xml version="1.0" encoding="UTF-8"?>
<payloads>
    <payload>
        <firstname>Sid</firstname>
        <secondname>Singh</secondname>
        <number>1</number>
    </payload>
    <payload>
        <firstname>Deepak</firstname>
        <secondname>Shahi</secondname>
        <number>2</number>
    </payload>
    <payload>
        <firstname>Shorya</firstname>
        <secondname>Garg</secondname>
        <number>3</number>
    </payload>
</payloads>

Please help to attain the solution.

Upvotes: 1

Views: 1760

Answers (1)

Bhaktuu
Bhaktuu

Reputation: 89

This link should help you

How to produce XML output using WCF service?

Or else please add attribute [Serializable()] to class Payloads and use the below code to:

Serialize(listObj)

public static string Serialize(object obj)
{
   var xs = new XmlSerializer(obj.GetType());
   var xml = new StringWriter();
   xs.Serialize(xml, obj);
   return xml.ToString();
}

Upvotes: 1

Related Questions