共用方式為


XML Web Service 的 XML 序列化

XML 序列化是用於 XML Web Service 架構、由 XmlSerializer 類別執行的基礎傳輸機制。若要控制由 XML Web Service 產生的 XML,您可將列於控制 XML 序列化的屬性控制編碼 SOAP 序列化的屬性中的屬性套用至用來建立 XML Web Service (.asmx) 的類別、傳回值、參數和檔案欄位。如需建立 XML Web Service 的詳細資訊,請參閱使用 ASP.NET 建置 XML Web Service

常值和編碼樣式

由 XML Web Service 產生的 XML 可利用下列兩種方式之一格式化:常值或編碼,如自訂 SOAP 訊息中的說明。因此控制 XML 序列化的屬性有兩組。列於控制 XML 序列化的屬性中的屬性是設計來控制常值樣式 XML。列於控制編碼 SOAP 序列化的屬性中的屬性則控制編碼樣式。藉由選擇性地套用這些屬性,您可設計傳回任一樣式或傳回兩種樣式的應用程式。除此之外,您可套用這些屬性 (視需要) 來傳回值和參數。

使用兩種樣式的範例

當您建立 XML Web Service 時,您可在方法上使用這兩組屬性。在下列範例中,名為 MyService 的類別包含兩個 XML Web Service 方法,分別是 MyLiteralMethodMyEncodedMethod。這兩個方法執行的功能相同:都傳回 Order 類別的執行個體。在 Order 類別中,XmlTypeAttributeSoapTypeAttribute 都套用至 OrderID 欄位,而兩個屬性都將其 ElementName 屬性設為不同值。

若要執行範例,請將程式碼貼至副檔名為 .asmx 的檔案,並且將檔案置於由 Internet Information Services (IIS) 管理的虛擬目錄中。從 HTML 瀏覽器 (例如 Internet Explorer),輸入電腦、虛擬目錄和檔案的名稱。

<%@ WebService Language="VB" Class="MyService" %>
Imports System
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
Public Class Order
    ' Both types of attributes can be applied. Depending on which type
    ' the method used, either one will affect the call.
    <SoapElement(ElementName:= "EncodedOrderID"), _
    XmlElement(ElementName:= "LiteralOrderID")> _
    public OrderID As String
End Class

Public Class MyService
    <WebMethod, SoapDocumentMethod> _
    public Function MyLiteralMethod() As Order 
        Dim myOrder As Order = New Order()
        return myOrder
    End Function
    <WebMethod, SoapRpcMethod> _
    public Function MyEncodedMethod() As Order 
        Dim myOrder As Order = New Order()
        return myOrder
    End Function
End Class
[C#]
<%@ WebService Language="C#" Class="MyService" %>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
public class Order{
    // Both types of attributes can be applied. Depending on which type
    // the method used, either one will affect the call.
    [SoapElement(ElementName = "EncodedOrderID")]
    [XmlElement(ElementName = "LiteralOrderID")]
    public String OrderID;
}
public class MyService{
    [WebMethod][SoapDocumentMethod]
    public Order MyLiteralMethod(){
        Order myOrder = new Order();
        return myOrder;
    }
    [WebMethod][SoapRpcMethod]
    public Order MyEncodedMethod(){
        Order myOrder = new Order();
        return myOrder;
    }
}

下列範例會呼叫 MyLiteralMethod。請注意,項目名稱會變更為「LiteralOrderID」。

<?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>
        <MyLiteralMethodResponse xmlns="http://tempuri.org/">
            <MyLiteralMethodResult>
                <LiteralOrderID>string</LiteralOrderID>
            </MyLiteralMethodResult>
        </MyLiteralMethodResponse>
    </soap:Body>
</soap:Envelope>

下列範例會呼叫 MyEncodedMethod。請注意,項目名稱是「EncodedOrderID」。

<?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="https://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:types="http://tempuri.org/encodedTypes" xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body soap:encodingStyle="https://schemas.xmlsoap.org/soap/encoding/">
        <tns:MyEncodedMethodResponse>
            <MyEncodedMethodResult href="#id1" />
        </tns:MyEncodedMethodResponse>
        <types:Order id="id1" xsi:type="types:Order">
            <EncodedOrderID xsi:type="xsd:string">string</EncodedOrderID>
        </types:Order>
    </soap:Body>
</soap:Envelope>

將屬性套用至傳回值

您也可將屬性套用至傳回值以控制命名空間、項目名稱等等。下列範例將 XmlElementAttribute 屬性套用至 MyLiteralMethod 方法的傳回值。這麼做可讓您控制命名空間和項目名稱。

    <WebMethod, SoapDocumentMethod> _
    public Function MyLiteralMethod() As _
    <XmlElement(Namespace:="http://www.cohowinery.com", _
    ElementName:= "BookOrder")> _
    Order 
        Dim myOrder As Order = New Order()
        return myOrder
    End Function
[C#]
    [return: XmlElement(Namespace = "http://www.cohowinery.com",
    ElementName = "BookOrder")]
    [WebMethod][SoapDocumentMethod]
    public Order MyLiteralMethod(){
        Order myOrder = new Order();
        return myOrder;
    }

當叫用 (Invoke) 時,程式碼會傳回如下的 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="https://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <MyLiteralMethodResponse xmlns="http://tempuri.org/">
            <BookOrder xmlns="http://www.cohowinery.com">
                <LiteralOrderID>string</LiteralOrderID>
            </BookOrder>
        </MyLiteralMethodResponse>
    </soap:Body>
</soap:Envelope>

套用至參數的屬性

您也可將屬性套用至參數以指定命名空間、項目名稱等等。下列範例會將參數加入至 MyLiteralMethodResponse 方法,並且將 XmlAttributeAttribute 屬性套用至參數。接著就設定參數的項目名稱和命名空間。

    <WebMethod, SoapDocumentMethod> _
    public Function MyLiteralMethod(<XmlElement _
    ("MyOrderID", Namespace:="https://www.microsoft.com")>ID As String) As _
    <XmlElement(Namespace:="http://www.cohowinery.com", _
    ElementName:= "BookOrder")> _
    Order 
        Dim myOrder As Order = New Order()
        myOrder.OrderID = ID
        return myOrder
    End Function
[C#]
    [return: XmlElement(Namespace = "http://www.cohowinery.com",
    ElementName = "BookOrder")]
    [WebMethod][SoapDocumentMethod]
    public Order MyLiteralMethod([XmlElement("MyOrderID", 
    Namespace="https://www.microsoft.com")] string ID){
        Order myOrder = new Order();
        myOrder.OrderID = ID;
        return myOrder;
    } 

SOAP 要求會像這樣。

<?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>
        <MyLiteralMethod xmlns="http://tempuri.org/">
            <MyOrderID xmlns="https://www.microsoft.com">string</MyOrderID>
        </MyLiteralMethod>
    </soap:Body>
</soap:Envelope>

將屬性套用至類別

如果您需要控制類別相關項目的命名空間,您可視需要套用 XmlTypeAttributeXmlRootAttributeSoapTypeAttribute。下列範例會將這三個屬性都套用至 Order 類別。

<XmlType("BigBookService"), _
SoapType("SoapBookService"), _
XmlRoot("BookOrderForm")> _
Public Class Order
    ' Both types of attributes can be applied. Depending on which
    ' the method used, either one will affect the call.
    <SoapElement(ElementName:= "EncodedOrderID"), _
    XmlElement(ElementName:= "LiteralOrderID")> _
    public OrderID As String
End Class
[C#]
[XmlType("BigBooksService", Namespace = "http://www.cpandl.com")]
[SoapType("SoapBookService")]
[XmlRoot("BookOrderForm")]
public class Order{
    // Both types of attributes can be applied. Depending on which
    // the method used, either one will affect the call.
    [SoapElement(ElementName = "EncodedOrderID")]
    [XmlElement(ElementName = "LiteralOrderID")]
    public String OrderID;
}

您可在檢查服務描述時看到套用 XmlTypeAttributeSoapTypeAttribute 的結果,如下列範例所示。

    <s:element name="BookOrderForm" type="s0:BigBookService" /> 
- <s:complexType name="BigBookService">
- <s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="LiteralOrderID" type="s:string" /> 
    </s:sequence>

- <s:schema targetNamespace="http://tempuri.org/encodedTypes">
- <s:complexType name="SoapBookService">
- <s:sequence>
    <s:element minOccurs="1" maxOccurs="1" name="EncodedOrderID" type="s:string" /> 
    </s:sequence>
    </s:complexType>
    </s:schema>

您也可在 HTTP GET 和 HTTP POST 結果中看到 XmlRootAttribute 的效果,如下所示。

<?xml version="1.0" encoding="utf-8"?>
<BookOrderForm xmlns="http://tempuri.org/">
    <LiteralOrderID>string</LiteralOrderID>
</BookOrderForm>

請參閱

XML 序列化 | 控制編碼 SOAP 序列化的屬性 | 使用 XML 序列化產生 SOAP 訊息 | XML 序列化簡介