第2章中曾經提到過,ASP.NET AJAX異步通信層在傳遞數據時默認采用JSON序列化方式,但同時也提供給我們以XML方式進行序列化的選項。
一般來講,如果某Web Service方法的返回值類型為XmlDocument或XmlElement,我們應該讓這類返回值以XML方式進行序列化。例如如下的這個Web Service方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlDocument GetXmlDocument()
{
string theString = "<persons>"
+ "<person><name>Tom</name><age>30</age></person>"
+ "<person><name>Jerry</name><age>20</age></person>"
+ "</persons>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(theString());
return xmlDoc;
}
圖3-36 服務器端XmlDocument類型在客戶端的結構
在客戶端得到返回的XML文檔對象之后,我們即可根據需求對其進行操作,限于篇幅,這里不贅。
對于非XmlDocument或XmlElement的類型,如果我們愿意,也可以選擇將其以XML的方式進行序列化。我們還是以前面定義的Employee類為例,如下Web Service方法就以XML序列化的方式返回一個Employee對象:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public Employee GetXMLFormatEmployee()
{
原文轉自:http://www.anti-gravitydesign.com