WCF 서비스 웹 사이트
인터페이스 IService
클래스를 사용하는 XML 출력이 필요합니다.
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>
해결책을 얻을 수 있도록 도와주세요.
이 링크는 당신을 도울 것입니다
WCF 서비스를 사용하여 XML 출력을 생성하는 방법은 무엇입니까?
또는 페이로드 클래스에 [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();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다