This article is from ASP.. Net Ajax programming Chapter II: client-related Microsoft Ajax library Chapter III asynchronous call of Web Services and class methods on pages. For more information, see other articles in this chapter.
3.8ToXMLSerialized data
As mentioned in chapter 2nd, the ASP. NET Ajax asynchronous communication layer uses the JSON serialization method by default when passing data, but it also provides the XML serialization option.
Generally, if the return value type of a Web service method is xmldocument or xmlelement, We Should serialize the returned values in XML format. For example, the following web service method:
[Webmethod]
[Scriptmethod (responseformat = responseformat. XML)]
PublicXmldocument getxmldocument ()
{
StringThestring ="<Persons>"
+"<Person> <Name> Tom </Name> <age> 30 </age> </person>"
+"<Person> <Name> Jerry </Name> <age> 20 </age> </person>"
+"</Persons>";
Xmldocument xmldoc =NewXmldocument ();
Xmldoc. loadxml (thestring );
ReturnXmldoc;
}
Note thatCodeIn bold,[Scriptmethod (responseformat = responseformat. XML)]This property sets the serialization method of the returned value of the getxmldocument () method to XML. In the callback function of the client, the returned client XML Document Object is shown in the structure 3-36 in the Visual Studio debugger.
Figure 3-36 Structure of the server-side xmldocument type on the client
After the client obtains the returned XML document object, we can perform operations on the object as needed, which is not redundant here.
For non-xmldocument or xmlelement types, if we want to, we can also choose to serialize them in XML format. Let's take the previously defined employee class as an example. The following web service method returns an employee object in XML serialization:
[Webmethod]
[Scriptmethod (responseformat = responseformat. XML)]
PublicEmployee getxmlformatemployee ()
{
Return NewEmployee (
12345,
"Dflying",
Dflying@some.com",
Int. Maxvalue
);
}
Similarly, the [scriptmethod (responseformat = responseformat. XML)] attribute (bold in the Code). In the client callback function, the returned JavaScript Object is shown in the structure 3-37 in the Visual Studio debugger.
Figure 3-37 structure of the employee object serialized in XML format
As you can see, the employee object is serialized into the following XML string:
<?XML Version= "1.0"?>
<Employee Xmlns: xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD= "Http://www.w3.org/2001/XMLSchema">
<ID>12345</ID>
<Name>Dflying</Name>
<Email>Dflying@some.com</Email>
<Salary>2147483647</Salary>
</Employee>
As mentioned above, add [system. web. script. serialization. scriptignore] attributes allow ASP. net Ajax asynchronous communication layer ignores this attribute when automatically generating client objects. However, this attribute is only applicable to the default serialization method, that is, the JSON method.
If you want to ignore an attribute of a complex object in the XML serialization method, you should add[System. xml. serialization. xmlignore]Attribute. Let's take ignoring the salary attribute in the employee class as an example to modify the implementation code of the employee class:
Private IntM_salary;
[System. xml. serialization. xmlignore]
Public IntSalary
{
Get {ReturnM_salary ;}
Set {m_salary =Value;}
}
At this time, we call the getxmlformatemployee () web service method from the client again, and we will see that the employee object is serialized into the following XML string, with no <salary/> node:
<? XML version ="1.0"?>
<Employee xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD =Http://www.w3.org/2001/XMLSchema">
<ID> 12345 </ID>
<Name> dflying </Name>
<Email> Dflying@some.com </Email>
</Employee>
If the returned value of a Web service method is of the string type and the Web service method serializes the returned value in XML format, the string is considered as a segment of an XML document by default, serialized in XML format. For example, the following web service method:
[Webmethod]
[Scriptmethod (responseformat = responseformat. XML)]
Public StringGetstring ()
{
StringThestring ="<Persons>"
+"<Person> <Name> Tom </Name> <age> 30 </age> </person>"
+"<Person> <Name> Jerry </Name> <age> 20 </age> </person>"
+"</Persons>";
ReturnThestring;
}
In the client callback function, the returned string object is shown in the 3-38 structure in the Visual Studio debugger. We can see that the content of a normal string is treated as an XML segment.
Figure 3-38 the content of a common string is treated as an XML segment by default.
Of course, there is a reason for ASP. NET ajax to make such a hypothesis, that is, the string may actually represent a piece of XML data. But if we need a normal string, isn't it helpful?
Fortunately, in the [scriptmethod] attributeXmlserializestringIf it is set to true, we can get a normal string, which avoids any automatic conversion for ASP. NET Ajax. Let's modify the previous getstring () method according to the following code. Note the bold part:
[Webmethod]
[Scriptmethod (responseformat = responseformat. XML,
Xmlserializestring =True)]
Public StringGetstring ()
{
StringThestring ="<Persons>"
+"<Person> <Name> Tom </Name> <age> 30 </age> </person>"
+"<Person> <Name> Jerry </Name> <age> 20 </age> </person>"
+"</Persons>";
ReturnThestring;
}
In the client callback function, the returned string object is automatically encoded and passed as a normal string, as shown in the highlighted section in 3-39.
Figure 3-39 passing common string data in XML format
Conclusion 3.9
ASP. net Ajax asynchronous communication layer for us to call the Server Web service or ASP in the client JavaScript. the class methods on the net page provide extremely convenient infrastructure, not only does not need to master any specific Ajax implementation principle, it even allows us to directly use a familiar C #-like syntax in JavaScript to complete asynchronous communication with the server.
This chapter first uses a simple exampleProgramThis section describes how to use ASP on the client. net Ajax asynchronous communication layer call is defined in the specific steps of the web service method, and then introduced from the client call defined in ASP. the process of class methods in the net page is designed to enable readers to view ASP. net Ajax asynchronous communication layer is the server-side web service and ASP. NET page class method generated by the client call proxy has a preliminary understanding.
There are many uncertainties in the running of Web applications. From the unstable network conditions to the carelessness of developers, any unpredictable problem will end a certain asynchronous call with failure. This chapter then introduces how to handle exceptions during asynchronous communication with the server through the example program.
To maintain necessary State information throughout the asynchronous call process, the asynchronous call model provided by ASP. NET Ajax contains support for passing user context. Through the user context object, we can easily access the state in the original call function. This chapter also describes how to implement these functions.
Later, this chapter introduces ASP in detail. the net Ajax asynchronous communication layer provides the client-side asynchronous call proxy function generated by the web service, including the complete syntax of the call proxy, the complete syntax of the success/failure callback function, and other common attributes and settings.
When you use ASP. Net Ajax asynchronous communication layer to asynchronously call Web Services, http post is used by default. However, to provide sufficient flexibility, the ASP. Net Ajax asynchronous communication layer also allows us to use http get for calling. This chapter then introduces how to use http get to call the sdk.
ASP. net Ajax asynchronous communication layer also provides powerful server. net and client JavaScript types can be automatically converted. We only need to add configuration, or even no configuration is required, in asynchronous communication, data including basic type, enumeration type, complex type, set (including generic set) type, and array type can be transmitted. This is the most important part in this chapter. Therefore, we provide a large number of sample programs and detailed analysis in this section, and strive to clarify the specific methods and precautions for passing the above types.
The ASP. NET Ajax asynchronous communication layer uses the JSON serialization method by default when passing data, but it also provides the XML serialization option. At the end of this chapter, we will introduce how to use XML as the data transfer format when the client interacts with the server.