XML serialization은 XmlSerializer 클래스에서 수행하는 XML 웹 서비스 아키텍처의 기본 전송 메커니즘입니다. XML 웹 서비스에서 생성된 XML을 제어하려면 XML Serialization을 제어하는 특성 과 인코딩된 SOAP Serialization을 제어하는 특성 모두에 나열된 특성을 적용하여 XML 웹 서비스(.asmx)를 만드는 데 사용되는 파일의 값, 매개 변수 및 필드를 반환합니다. XML 웹 서비스를 만드는 방법에 대한 자세한 내용은 ASP.NET 사용하여 XML Web Services를 참조하세요.
리터럴 및 인코딩된 스타일
XML 웹 서비스에서 생성된 XML은 SOAP 메시지 서식 사용자 지정에 설명된 대로 리터럴 또는 인코딩된 두 가지 방법 중 하나로 서식을 지정할 수 있습니다. 따라서 XML serialization을 제어하는 두 가지 특성 집합이 있습니다. XML Serialization을 제어하는 특성에 나열된 특성은 리터럴 스타일 XML을 제어하도록 설계되었습니다. 인코딩된 SOAP Serialization을 제어하는 특성에 나열된 특성은 인코딩된 스타일을 제어합니다. 이러한 특성을 선택적으로 적용하여 애플리케이션을 맞춤화하여 스타일을 반환하거나 둘 다 반환할 수 있습니다. 또한 이러한 특성은 값과 매개 변수를 반환하는 데 적절하게 적용할 수 있습니다.
두 스타일 사용 예제
XML 웹 서비스를 만들 때 메서드에서 두 특성 집합을 모두 사용할 수 있습니다. 다음 코드 예제에서 명명된 MyService 클래스에는 두 개의 XML 웹 서비스 메서드 MyLiteralMethod 및 MyEncodedMethod. 두 메서드 모두 클래스의 인스턴스를 반환하는 동일한 함수를 수행합니다 Order .
Order 클래스에서 XmlTypeAttribute 및 SoapTypeAttribute 속성이 모두 OrderID 필드에 적용되고, 각 특성의 ElementName 속성이 서로 다른 값으로 설정됩니다.
예제를 실행하려면 코드를 .asmx 확장자를 사용하여 파일에 붙여넣고 IIS(인터넷 정보 서비스)에서 관리하는 가상 디렉터리에 파일을 배치합니다. 웹 브라우저에서 컴퓨터, 가상 디렉터리 및 파일의 이름을 입력합니다.
<%@ 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
<%@ 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="http://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="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:types="http://tempuri.org/encodedTypes" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body soap:encodingStyle="http://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
[return: XmlElement(Namespace = "http://www.cohowinery.com",
ElementName = "BookOrder")]
[WebMethod][SoapDocumentMethod]
public Order MyLiteralMethod(){
Order myOrder = new Order();
return myOrder;
}
호출될 때 코드는 다음과 유사한 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>
<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:="http://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
[return: XmlElement(Namespace = "http://www.cohowinery.com",
ElementName = "BookOrder")]
[WebMethod][SoapDocumentMethod]
public Order MyLiteralMethod([XmlElement("MyOrderID",
Namespace="http://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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MyLiteralMethod xmlns="http://tempuri.org/">
<MyOrderID xmlns="http://www.microsoft.com">string</MyOrderID>
</MyLiteralMethod>
</soap:Body>
</soap:Envelope>
클래스에 특성 적용
클래스와 관련된 요소의 네임스페이스를 제어해야 할 경우, 적절하게 XmlTypeAttribute, XmlRootAttribute, 및 SoapTypeAttribute를 적용할 수 있습니다. 다음 코드 예제에서는 세 가지를 모두 클래스에 적용합니다 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
[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;
}
아래 코드 예제에서 볼 수 있듯이 서비스 설명을 검사할 때 XmlTypeAttribute과 SoapTypeAttribute 적용 결과를 확인할 수 있습니다.
<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>
</s:complexType>
이 효과는 XmlRootAttribute 다음과 같이 HTTP GET 및 HTTP POST 결과에서도 확인할 수 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<BookOrderForm xmlns="http://tempuri.org/">
<LiteralOrderID>string</LiteralOrderID>
</BookOrderForm>
참고하십시오
.NET