asp.net 2.0 Ajax

Source: Internet
Author: User
Tags serialization visual studio

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
     );
   }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.