如何通过WCF服务网站获得XML响应?

如何通过WCF服务网站获得XML响应?,xml,wcf,Xml,Wcf,我需要使用WCF服务网站的XML输出 接口IServiceclass public interface IService { [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetPay")] Payload GetPay

我需要使用WCF服务网站的XML输出
接口
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 Web.congig文件代码

<?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>

我需要以下格式的输出。请帮忙

<?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>

希德
辛格
1.
迪帕克
沙希
2.
山、绍里亚
加格
3.

请帮助获得解决方案。

此链接应该对您有所帮助

或者,请将属性[Serializable()]添加到类有效负载,并使用以下代码:

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();
}
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();
}