Light  Rain serena

«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

期待

超级好友 Rachel


我的分类(专题)

首页(262)
知识积累(40)
乐瑟温柔(108)
经验杂谈(20)
良辰吉日(2)
杂七杂八(57)
天景共赏(10)
感触文字(23)


最新日志
sunshine girl
过云雨
summer whisper
边走边唱
岛歌
不必在乎我是谁
如果有来生
Fing Fing 下
大明宫
我没有魅力

最新回复
回复:边界类、控制类、实体类
回复:边界类、控制类、实体类
回复:边界类、控制类、实体类
回复:《极地特快》插曲《When Chr
回复:边界类、控制类、实体类
回复:不必在乎我是谁
回复:边界类、控制类、实体类
回复:如果有来生
回复:Fing Fing 下
回复:我没有魅力

留言板
签写新留言

老大&老师,我想你.....
好文好多阿
886电台 好听
cai
您的子域名已开通
hi
祝福
祝福

你好

统计
blog名称:小雨
日志总数:262
评论数量:1273
留言数量:15
访问次数:4667591
建立时间:2005年1月8日

链接

有事?
音乐,永不厌倦
宁静的心情驿站
我的声音
TIPOblog
tipo bbs
appleVB
网页设计
英语学习2
英语学习3
FLASH
网络书籍
网络电台



我的博客朋友
最有技术的blog
xenium
花差花差
瓜少
span
a lai
琦少
陈少
v
阿当
http://www.ypb.cc/
痞子若鱼
另一个同行
年轻人bcims

 




W3CHINA Blog首页    管理页面    写新日志    退出

[知识积累]自定义 SOAP 消息
小雨 发表于 2005/4/4 21:00:00

对于使用 ASP.NET 创建的 XML Web services 和它们的使用 SOAP 与 XML Web services 方法进行通信的客户端来说,可以使用一种复杂的机制来控制发送到服务器和从服务器接收的 SOAP 消息的格式。SOAP 规定发送到 XML Web services 和从 XML Web services 发送回的 SOAP 消息的内容必须为 XML 格式。但是,它并没有严格地规定 XML 的格式设置。为了提供一个使用接收不同的格式设置的 XML Web services 的机制,ASP.NET 提供了一个基于属性的机制来控制 SOAP 消息中 XML 的格式。另外,还提供了指定特定元素和通过网络发送的 SOAP 属性名的基于属性的机制,用来在更精细的细节级别上控制 SOAP。 XML Web services 和它们的客户端之间的通信在很大程度上是由两个行业标准指定的:SOAP 和 Web 服务描述语言 (WSDL)。SOAP 为出现在 Body 元素下的数据定义格式设置方案,并为如何在 Body 元素中对参数进行格式设置定义格式设置方案。前者被称为 SOAP 第 7 节或直接称为 RPC。有关 SOAP 规范的详细信息,请参见 W3C Web 站点 (http://www.w3.org/TR/SOAP)。后者被称为 SOAP 第 5 节或直接称为 Encoded。WSDL 用于描述 XML Web services 所需的 SOAP 消息,WSDL 允许 XML Web services 声明它们接受带有编码参数的 RPC 消息,但它还定义了两个其他术语:Literal 和 Document。Literal(与 Encoded 类似)是指参数的格式设置方式。Document(与 RPC 类似)是指全部 Body 元素的格式设置方式。 自定义 XML Web services 客户端发送的 SOAP 下表概述了使用 ASP.NET 创建的 XML Web services 支持的格式设置样式以及完成每个特定组合的属性。带有 Service 后缀的属性可以应用于实现 XML Web services 的类以设置该类中 XML Web services 方法的默认格式设置样式。带有 Method 后缀的属性只能应用于 XML Web services 方法或调用 XML Web services 方法的代理类中的方法。每个组合的详细信息在下表中都有所介绍。     整个 SOAP 体格式设置 参数格式设置 基于 Document 的 SOAP 消息 基于 RPC 的 SOAP 消息(根据 SOAP 第 7 节) Literal — 基于每个参数的 XSD 架构 SoapDocumentMethod 或 SoapDocumentService Use=Literal 这是默认设置。 不受支持。 Encoded - SOAP 第 5 节编码规则 SoapDocumentMethod 或 SoapDocumentService Use=Encoded SoapRpcMethod 或 SoapRpcService 修改参数格式设置 设计 XML Web services 时首先要作出的决定之一就是您要如何对 SOAP 请求中的 XML 进行编码。明确地说,您是要 XML 文档严格遵循 XSD 架构还是要遵循 SOAP 规范第 5 节和第 7 节中概述的格式设置规则?SOAP 规范第 5 节和第 7 节中的格式设置规则考虑到了变体。这样,使用 SOAP 编码规则的 SOAP 请求的接收方必须处理所有可能的变体。通过定义 XML Web services 方法的 XSD 架构,可以具体地定义 SOAP 请求中需要发送的确切内容。使用 ASP.NET 创建的 XML Web services 默认为使用基于架构传递的文档。 因为 XML Web services 方法的参数可以组成 SOAP 请求或响应中传递的大部分数据,所以参数映射到 XML 元素的方式决定了 XML 文档的外观。Web 服务描述语言 (WSDL) 定义了两种参数格式设置样式:Encoded 和 Literal。Encoded 是指使用 SOAP 规范第 5 节中概述的 SOAP 编码对参数进行格式设置。Literal 是指使用每个参数预定义的 XSD 架构将参数映射到 XML 元素。 使用 XML Web services 客户端,您可以选择如何将参数映射到 XML 元素以匹配 XML Web services 需要的形式。XML Web services 同时支持 Literal 和 Encoded 参数的格式设置样式。该支持根据 XML Web services 方法格式设置选择有所变化。有关更多信息,请参见修改整个 SOAP 体的格式设置。 请注意,虽然 ASP.NET 提供了一个广泛的结构用来控制 XML 格式设置的方式,但是并不保证参数序列化的顺序。 指定 Literal 参数格式设置 将 SoapDocumentMethod 特性应用于代理类中的方法,将 Use 属性设置为 SoapBindingUse.Literal。 SoapBindingUse 枚举指定使用 ASP.NET 创建的 XML Web services 可用的参数格式设置样式。 [C#] [SoapDocumentMethod( "http://www.contoso.com/DocumentLiteral", RequestNamespace="http://www.contoso.com", ResponseNamespace="http://www.contoso.com", Use=SoapBindingUse.Literal)] public string DocumentLiteral(Address1 address, bool useZipPlus4) { [Visual Basic] <SoapDocumentMethod( _ "http://www.contoso.com/DocumentLiteral", _ RequestNamespace:="http://www.contoso.com", _ ResponseNamespace:="http://www.contoso.com", _ Use:=SoapBindingUse.Literal)> _ Public Function DocumentLiteral(ByVal address As Address1, _ ByVal useZipPlus4 As Boolean) As String 对 DocumentLiteral XML Web services 方法的 SOAP 请求的 XML 部分如下。参数驻留在 Body 元素中并编码为自包含的 XML 文档,这时因为它们是指 XSD 架构。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DocumentLiteral xmlns="http://www.contoso.com"> <address> <Street>One Microsoft Way</Street> <City>Redmond</City> <Zip>98052</Zip> </address> <useZipPlus4>True</useZipPlus4> </DocumentLiteral> </soap:Body> </soap:Envelope> 指定 Encoded 参数格式设置 将 SoapDocumentMethod 特性或 SoapRpcMethod 特性应用于代理类中的方法,将 Use 属性设置为 SoapBindingUse.Encoded。 不像 Literal 参数格式设置样式,Encoded 参数格式设置样式可以和两种 XML Web services 方法格式设置样式一起使用。有关 XML Web services 方法格式设置样式的更多详细信息,请参见修改整个 SOAP 体的格式设置 [C#] [SoapDocumentMethod("http://www.contoso.com/DocumentEncoded", RequestNamespace="http://www.contoso.com", ResponseNamespace="http://www.contoso.com", Use=SoapBindingUse.Encoded)] public string DocumentEncoded(Address address, bool useZipPlus4) { [Visual Basic] <SoapDocumentMethod("http://www.contoso.com/DocumentEncoded", _ RequestNamespace:="http://www.contoso.com", _ ResponseNamespace:="http://www.contoso.com", _ Use:=SoapBindingUse.Encoded)> _ Public Function DocumentEncoded(ByVal address As Address, _ ByVal useZipPlus4 As Boolean) As String 对 DocumentEncoded 服务方法的 SOAP 请求的 XML 部分如下。注意参数的表示形式与 Literal 格式设置样式有很大的不同,因为它们是使用 SOAP 规范第 5 节中概述的编码规则进行格式设置的。请特别注意 address 参数,它不是一个简单数据类型。 <?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:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.contoso.com" xmlns:tnsTypes="http://www.contoso.com/encodedTypes" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <tnsTypes:DocumentEncoded> <address href="#1" /> <useZipPlus4>boolean</useZipPlus4> </tnsTypes:DocumentEncoded> <tnsTypes:Address id="1"> <Street id="2">string</Street> <City id="3">string</City> <Zip id="4">string</Zip> </tnsTypes:Address> </soap:Body> </soap:Envelope> 修改整个 SOAP 体的格式设置 WSDL 定义两种在 SOAP 请求或 SOAP 响应的 Body 元素中对 XML Web services 方法(WSDL 称之为操作)进行格式设置的样式:RPC 和 Document。在 ASP.NET 中,Document 和 RPC 这两种格式设置样式都支持。Document 是默认的。 RPC 样式是指根据 SOAP 规范中对 RPC 使用 SOAP 的内容(也就是通常所说的 SOAP 规范的第 7 节)对 Body 元素进行格式设置。该样式规定所有参数都封装在单个元素中,按 XML Web services 方法进行命名,该元素中的每个元素都表示一个参数,这些元素按它们各自的参数名进行命名。它还说明了如何对 SOAP 响应进行格式设置的规则。 Document 样式是指将 Body 元素格式化为 Body 元素下的一个或多个连续的消息部分。形成个别消息部分的确切方式是由 SoapDocumentMethod 特性的 Use 和 ParameterStyle 属性决定的。Use 属性确定是否将参数格式化为 Encoded 或 Literal。ParameterStyle 确定是将参数封装在 Body 元素下的单个消息部分中,还是每个参数都是单独的消息部分。有关设置 ParameterStyle 属性的详细信息,请参见修改是否将参数封装在一个 XML 元素中。 指定 Document 格式设置样式 将 SoapDocumentMethod 属性应用于调用相关 XML Web services 方法的代理类中的方法。 使用 Document 格式设置样式,使用 ASP.NET 创建的 XML Web services 支持使用 Literal 和 Encoded 参数格式设置样式。下面的示例结合了 Document 方法格式设置样式和 Literal 参数格式设置样式。 [C#] [SoapDocumentMethod("http://www.contoso.com/DocumentWrappedLiteral", RequestNamespace="http://www.contoso.com", ResponseNamespace="http://www.contoso.com", Use=SoapBindingUse.Literal)] public string DocumentWrappedLiteral(Address MyAddress, bool useZipPlus4) { [Visual Basic] <SoapDocumentMethod("http://www.contoso.com/DocumentWrappedLiteral", _ RequestNamespace:="http://www.contoso.com", _ ResponseNamespace:="http://www.contoso.com", _ Use:=SoapBindingUse.Literal)> _ Public Function DocumentWrappedLiteral(ByVal MyAddress As Address, _ ByVal useZipPlus4 As Boolean)As String 与 Document 格式设置样式的处理方式相同,XSD 架构是在定义 SOAP 请求和 SOAP 响应的服务说明中定义的。下面是 DocumentWrappedLiteral XML Web services 方法 SOAP 请求的服务说明中的一段摘录。因为 DocumentWrappedLiteral XML Web services 方法的第一个参数是一个类,并且指定了 Literal 参数格式设置样式,所以为 address 类型创建一个 XSD 架构。 <s:element name="DocumentWrappedLiteral"> <s:complexType> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="MyAddress" nillable="true" type="s0:Address" /> <s:element minOccurs="1" maxOccurs="1" name="useZipPlus4" type="s:boolean" /> </s:sequence> </s:complexType> </s:element> <s:complexType name="Address"> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="Street" nillable="true" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="City" nillable="true" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="Zip" nillable="true" type="s:string" /> </s:sequence> </s:complexType> 在给定服务说明中定义的 XSD 架构的情况下,对 DocumentWrappedLiteral 服务方法的 SOAP 请求的 XML 部分如下。注意,SOAP 请求中位于 Body 元素下的 XML 元素与 XSD 架构中定义的元素相匹配。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DocumentWrappedLiteral xmlns="http://www.contoso.com"> <MyAddress> <Street>string</Street> <City>string</City> <Zip>string</Zip> </MyAddress> <useZipPlus4>boolean</useZipPlus4> </DocumentWrappedLiteral> </soap:Body> </soap:Envelope> 指定 RPC 格式设置样式 将 SoapRpcMethod 属性应用于调用相关 XML Web services 方法的代理类中的方法。 使用 RPC 格式设置样式,ASP.NET 只支持 Encoded 参数格式设置样式。因此,下面的代码示例不为参数格式设置指定 Use 属性,这是因为 SoapRpcMethod 特性不具有 Use 属性。 [C#] [SoapRpcMethodAttribute("http://www.contoso.com/Rpc", RequestNamespace="http://www.contoso.com", ResponseNamespace="http://www.contoso.com")] public Address Rpc(Address address, bool useZipPlus4) { [Visual Basic] <SoapRpcMethodAttribute("http://www.contoso.com/Rpc", _ RequestNamespace:="http://www.contoso.com", _ ResponseNamespace:="http://www.contoso.com")> _ Public Function Rpc(ByVal address As Address, _ ByVal useZipPlus4 As Boolean) As Address 服务说明中没有为上面示例中对 Rpc 方法的 SOAP 请求或 SOAP 响应严格地定义 XSD 架构,而只定义了组成它们的部分。因此,查看 Rpc 方法的 SOAP 请求,注意参数封装在一个元素中,它们是使用 Encoded 参数格式设置编码的。 <?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:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.contoso.com" xmlns:tnsTypes="http://www.contoso.com/encodedTypes" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <tns:Rpc> <address HREF="#1" TARGET="_self" /> <useZipPlus4>boolean</useZipPlus4> </tns:Rpc> <tnsTypes:Address id="1"> <Street id="2">string</Street> <City id="3">string</City> <Zip id="4">string</Zip> </tnsTypes:Address> </soap:Body> </soap:Envelope> 修改是否将参数封装在一个 XML 元素中 当 XML Web services 方法的参数映射到驻留在 SOAP 消息的 Body 元素中的 XML 元素时,可以将这些参数封装在一个 XML 元素中。正如您所看到的,RPC 总是将参数封装在一个元素中;但是当使用 Document 格式设置样式时,您将有所选择。在服务说明中,参数被映射到一段 SOAP 消息(称为消息部分)。当参数封装在一个 XML 元素中时,这些参数映射到单个消息部分。相反,如果这些参数未封装在一个 XML 元素中,则它们可以组成多个消息部分。 指定将参数封装在一个 XML 元素中 将 SoapDocumentMethod 特性应用于调用相关 XML Web services 方法的代理类中的方法,将 ParameterStyle 属性设置为 Wrapped。 下面的代码示例将 ParameterStyle 设置为 Wrapped。它还将参数格式设置样式设置为 Literal。 [C#] [SoapDocumentMethod("http://www.contoso.com/DocumentWrappedLiteral", RequestNamespace="http://www.contoso.com", ResponseNamespace="http://www.contoso.com", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] public string DocumentWrappedLiteral(Address1 address, bool useZipPlus4) { [Visual Basic] <SoapDocumentMethod("http://www.contoso.com/DocumentWrappedLiteral", _ RequestNamespace:="http://www.contoso.com", _ ResponseNamespace:="http://www.contoso.com", _ Use:=SoapBindingUse.Literal, _ ParameterStyle:=SoapParameterStyle.Wrapped)> _ Public Function DocumentWrappedLiteral(ByVal address As Address1, _ ByVal useZipPlus4 As Boolean) As String SOAP 请求的 XML 部分将参数封装在一个元素中,该元素默认按 XML Web services 方法进行命名。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DocumentWrappedLiteral xmlns="http://www.contoso.com"> <MyAddress> <Street>string</Street> <City>string</City> <Zip>string</Zip> </MyAddress> <useZipPlus4>boolean</useZipPlus4> </DocumentWrappedLiteral> </soap:Body> </soap:Envelope> SOAP 响应的 XML 部分封装 XML Web services 方法的输出参数中,包括元素中的结果。默认情况下,封装元素的名称是其后追加了 Response 的 XML Web services 方法的名称。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DocumentWrappedLiteralResponse xmlns="http://www.contoso.com"> <DocumentWrappedLiteralResult>string </DocumentWrappedLiteralResult> </DocumentWrappedLiteralResponse> </soap:Body> </soap:Envelope> 指定紧随 Body 元素之后的参数 将 SoapDocumentMethod 特性应用于调用相关 XML Web services 方法的代理类中的方法,将 ParameterStyle 属性设置为 Bare。 下面的由 Wsdl.exe 生成的示例将 ParameterStyle 设置为 Bare,同时将参数格式设置样式设置为 Literal。因为不能在封装所有参数的元素中指定命名空间,所以必须单独为每个参数和返回值指定命名空间。将 XmlElementAttribute 应用于每个参数和返回值并设置 Namespace 属性便可以完成该操作。 [C#] [SoapDocumentMethod( "http://www.contoso.com/DocumentBareLiteral", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Bare)] [return: XmlElement(Namespace="http://www.contoso.com", IsNullable=true)] public string DocumentBareLiteral( [XmlElement(Namespace="http://www.contoso.com", IsNullable=true)] Address1 MyAddress, [XmlElement(Namespace="http://www.contoso.com", IsNullable=false)] bool useZipPlus4) { [Visual Basic] <SoapDocumentMethod( _ http://www.contoso.com/DocumentBareLiteral", _ Use:=SoapBindingUse.Literal, _ ParameterStyle:= SoapParameterStyle.Bare)> _ Public Function DocumentBareLiteral( _ ByVal <XmlElement([Namespace]:="http://www.contoso.com", _ IsNullable:=true)> _ MyAddress As Address1, _ ByVal <XmlElement([Namespace]:="http://www.contoso.com", _ IsNullable:=false)> _ useZipPlus4 As Boolean) _ As <XmlElement([Namespace]:="http://www.contoso.com", _ IsNullable:=true)> _ String SOAP 请求中参数映射到的 XML 元素紧跟在 Body 元素之后,每一个都指定一个命名空间。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <MyAddress xmlns="http://www.contoso.com"> <Street>string</Street> <City>string</City> <Zip>string</Zip> </MyAddress> <useZipPlus4 xmlns="http://www.contoso.com">boolean</useZipPlus4> </soap:Body> </soap:Envelope> 输出参数(包括返回值)映射到 SOAP 响应中 Body 元素之后的 XML 元素。默认情况下,返回值元素名称是带有 Result 后缀的 XML Web services 方法的名称。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <DocumentBareLiteralResult xmlns="http://www.contoso.com"> string</DocumentBareLiteralResult> </soap:Body> </soap:Envelope> 自定义 XML Web services 方法所需的 SOAP 如果需要更改使用 ASP.NET 创建的 XML Web services 所需的 SOAP,可以使用 ASP.NET 创建的 XML Web services 客户端所用的相同机制。可以控制参数与 XML 元素之间的映射,控制表示 XML Web services 方法的 XML 元素,以及映射的元素是否封装在一个元素中。可以基于方法使用相同的属性,即 SoapDocumentMethod 和 SoapRpcMethod。另外,您可以通过将相应的 SoapDocumentService 和 SoapRpcService 属性应用于 XML Web services 在 XML Web services 级别设置默认的格式化设置。将 SoapDocumentService 属性应用于 XML Web services 可以将其 XML Web services 方法的默认方法格式设置样式设置为 Document。同样,SoapRpcService 属性将默认样式设置为 RPC。 为 XML Web services 设置默认的方法格式设置样式 将 SoapRpcService 属性或 SoapDocumentService 属性应用于实现 XML Web services 的类。 下面的代码示例将方法格式样式设置为 Document,将默认参数格式设置为 Literal,并指定参数必须封装在单个元素中。 [C#] <%@ WebService Language="C#" Class="SoapDocumentServiceSample" %> using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Services.Description; [SoapDocumentService(Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] [WebService(Namespace="http://www.contoso.com")] public class SoapDocumentServiceSample { [ WebMethod ] public string UseDefaultEncoding(Address MyAddress, bool useZipPlus4) { return "Use the default encodings for this XML Web service."; } } [Visual Basic] <%@ WebService Language="VB" Class="SoapDocumentServiceSample" %> Imports System.Web.Services Imports System.Xml.Serialization Imports System.Web.Services.Protocols Imports System.Web.Services.Description < SoapDocumentService(Use := SoapBindingUse.Literal, _ ParameterStyle := SoapParameterStyle.Wrapped)> _ Public Class SoapDocumentServiceSample < WebMethod > _ Public Function UseDefaultEncoding(MyAddress as Address, _ useZipPlus4 As Boolean) As String Return "Use the default formattings for this XML Web service." End Function End Class UseDefaultEncoding XML Web services 方法所需的 SOAP 请求的 XML 部分如下。 <?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="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <UseDefaultEncoding xmlns="http://www.contoso.com"> <MyAddress> <Street>string</Street> <City>string</City> <Zip>string</Zip> </MyAddress> <useZipPlus4>boolean</useZipPlus4> </UseDefaultEncoding> </soap:Body> </soap:Envelope> 用 XML 序列化自定义 SOAP 消息 除了指定参数格式设置、XML Web services 方法的格式设置样式以及是否将参数封装在一个元素中,还可以用 XML 序列化直接自定义 SOAP 消息。在 XmlElement 属性中,您已经在一定程度上了解这方面的操作,但是 System.Xml.Serialization 命名空间包括许多用来操作 XML 的属性。假定 SOAP 使用 XML 对发送到 XML Web services 和从 XML Web services 发送回的数据进行编码,则 System.Xml.Serialization 命名空间中的属性非常适合于自定义发送到 XML Web services 和从 XML Web services 发送回的 SOAP。有关更多信息,请参见 System.Xml.Serialization。 指定表示参数的 XML 元素的名称 将 XmlElement 属性应用于参数,为该元素指定所需的名称,或者如果参数格式设置被设置为 Literal,还可以指定命名空间。如果参数格式设置被设置为 Encoded,则将 SoapElement 属性应用于该参数。 下面的代码示例想要表示参数的元素名称为 MyAddressElement、MyZipElement 和 ReturnValueElement。它还希望表示返回值的元素名称为 ReturnValueElement。示例中的 XML Web services 方法格式设置为 Document,这是 ASP.NET 的默认设置。 [C#] <%@ WebService Language="C#" Class="SoapDocumentServiceSample" %> using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Serialization; [WebService(Namespace="http://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(); } } [Visual Basic] <%@ WebService Language="VB" Class="SoapDocumentServiceSample" %> Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Xml.Serialization <WebService(Namespace := "http://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 XML Web services 需要以下 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="http://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> 请参见 介绍 XML 序列化 | System.Xml.Serialization 命名空间 | 使用 SOAP 扩展更改 SOAP 消息 | SoapDocumentMethodAttribute | SoapRpcMethodAttribute | SoapDocumentServiceAttribute | SoapRpcServiceAttribute | 使用 ASP.NET 生成 XML Web services | 生成 XML Web services 客户端

阅读全文(5927) | 回复(0) | 编辑 | 精华


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144758896 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号