【问题标题】:How to get XML response through WCF Service Website?如何通过 WCF 服务网站获取 XML 响应?
【发布时间】:2017-06-20 15:41:22
【问题描述】:

我需要使用 WCF 服务网站的 XML 输出
接口IService

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

我的服务等级在下面

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

我的 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>

请帮忙解决。

【问题讨论】:

    标签: xml wcf


    【解决方案1】:

    这个链接应该对你有帮助

    How to produce XML output using WCF service?

    或者,请在类 Payloads 中添加属性 [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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多