As mentioned in the 2nd chapter, the ASP.NET Ajax asynchronous communication layer defaults to JSON serialization when passing data, but it also provides us with the option of serializing in an XML format.
In general, if the return value type of a Web service method is XmlDocument or XmlElement, we should have this kind of return value serialized in XML. For example, the following Web service method:
[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;
}
Note The bold part of the above code, [Scriptmethod (Responseformat = responseformat.xml)] This property sets the serialization of the GetXmlDocument () method to return the value to XML. In the client's callback function, the returned client XML Document object is displayed in the Visual Studio debugger in the structure shown in Figure 3-36.
Figure 3-36 Server-side XmlDocument type in client structure
After the client gets the returned XML Document object, we can manipulate it according to the requirements, which is not superfluous here.
For types that are not XmlDocument or XmlElement, you can optionally serialize them as XML, if we wish. As an example of the employee class defined previously, the following Web service method returns an Employee object in XML serialization:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public Employee GetXMLFormatEmployee()
{
return new Employee(
12345,
"Dflying",
"Dflying@some.com",
int.MaxValue
);
}