方法 : XML シリアル化によって SOAP メッセージをカスタマイズする
このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.
System.Web.Serialization 名前空間には、XML シリアル化を制御する多くの属性があり、Web サービス メソッドのパラメーターや戻り値に適用できます。このトピックでは、XmlElementAttribute 属性の使用方法について説明します。
パラメーターを表す XML 要素の名前を指定するには
パラメーターに XmlElement 属性を適用します。これは、要素に付ける名前を指定します。また、パラメーターの書式が Literal に設定されている場合は、オプションで名前空間に付ける名前を指定できます。パラメーターの書式が Encoded に設定されている場合は、パラメーターに SoapElement 属性を適用します。
次のコード例では、パラメーターを表す要素名として
MyAddressElement
、MyZipElement
、およびReturnValueElement
を要求します。また、戻り値を表す要素名としてReturnValueElement
を要求します。コード例の Web サービス メソッドの書式は、ASP.NET の既定値である Document です。<%@ WebService Language="C#" Class="SoapDocumentServiceSample" %> using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; [WebService(Namespace="https://www.contoso.com")] public class SoapDocumentServiceSample { [ WebMethod ] [ return: XmlElement("ReturnValueElement",IsNullable=false)] public Address ValidateAddress( [XmlElement("MyAddressElement")] Address MyAddress, [XmlElement("MyZipElement")] bool useZipPlus4) { useZipPlus4 = true; return new Address(); } }
<%@ WebService Language="VB" Class="SoapDocumentServiceSample" %> Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Xml.Serialization <WebService(Namespace := "https://www.contoso.com")> _ Public Class SoapDocumentServiceSample < WebMethod > _ Public Function ValidateAddress( _ <XmlElement("MyAddressElement")> MyAddress As Address, _ <XmlElement("MyZipElement")> useZipPlus4 As Boolean) As <XmlElement("ReturnValueElement",IsNullable :=false)> _ Address useZipPlus4 = True Return new Address() End Function End Class
Web サービスは次の SOAP 要求を予期します。要素の名前は、パラメーター名ではなく、XmlElement 属性で指定した名前と一致します。
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ValidateAddress xmlns="http://tempuri.org/"> <MyAddressElement> <Street>string</Street> <City>string</City> <Zip>string</Zip> </MyAddressElement> <MyZipElement>boolean</MyZipElement> </ValidateAddress> </soap:Body> </soap:Envelope>
参照
リファレンス
System.Xml.Serialization Namespace
SoapDocumentMethodAttribute
SoapRpcMethodAttribute
SoapDocumentServiceAttribute
SoapRpcServiceAttribute
概念
SOAP 拡張機能を使用した SOAP メッセージの変更
XML Web サービス クライアントの作成
その他のリソース
SOAP メッセージの書式のカスタマイズ
Introducing XML Serialization
ASP.NET を使用した XML Web サービス