• Data is returned in JSON format by default.
• Use scriptmethodattribute to mark
-Set the responseformat attribute to XML.
-The Content-Type of response will be text/XML
• You can concatenate XML data and output data using strings.
• XML-related types can be returned
-Xmldocument and xmlelement
• Xmlserializer output will be used when normal objects are returned
-Powerful XML serialization functions in. Net can be used.
Aspx < Form ID = " Form1 " Runat = " Server " >
< ASP: scriptmanager runat = " Server " ID = " Scriptmanager1 " Scriptmode = " Debug " >
< Services >
< ASP: servicereference path = " Services/returnxmlservice. asmx " Inlinescript = " True " />
</ Services >
</ ASP: scriptmanager >
< Input type = " Button " Value = " Getxmldocument " Onclick = " Returnxmlservice. getxmldocument (onsucceeded ); " /> < BR /> < BR />
< Input type = " Button " Value = " Getxmlelement " Onclick = " Returnxmlservice. getxmlelement (onsucceeded ); " /> < BR /> < BR />
< Input type = " Button " Value = " Getemployee " Onclick = " Returnxmlservice. getemployee (onsucceeded ); " /> < BR /> < BR />
< Input type = " Button " Value = " Getxmlstring " Onclick = " Returnxmlservice. getxmlstring (onsucceeded ); " /> < BR /> < BR />
< Input type = " Button " Value = " Getserializedstring " Onclick = " Returnxmlservice. getserializedstring (onsucceeded ); " />
< Script Language = " Javascript " Type = " Text/JavaScript " >
Function onsucceeded (result)
{
Alert (result. XML );
}
</ Script >
</ Form >
returnxmlservice. asmx <% @ WebService language = " C # " class = " returnxmlservice " %>
UsingSystem;
UsingSystem. Web;
UsingSystem. Web. Services;
UsingSystem. Web. Services. Protocols;
UsingSystem. Web. Script. Services;
UsingSystem. xml;
[WebService (namespace = " Http://tempuri.org/ " )]
[Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
[Scriptservice]
Public Class Returnxmlservice: system. Web. Services. WebService
{
[Webmethod]
[Scriptmethod (responseformat = Responseformat. XML)]
Public Xmlnode getxmldocument ()
{
Xmldocument Doc = New Xmldocument ();
Doc. loadxml ( " <Employee> <Name> Jeffrey Zhao </Name> <salary> 1000 </salary> </employee> " );
ReturnDoc;
}
[webmethod]
[scriptmethod (responseformat = responseformat. XML)]
Public xmlnode getxmlelement ()
{< br> xmldocument doc = New xmldocument ();
Doc. loadxml ( " Jeffrey Zhao 1000 " );
return Doc. documentelement;
}
[webmethod]
[scriptmethod (responseformat = responseformat. XML)]
Public maid ()
{< br> return New employee ( " Jeffrey Zhao " , 1000 );
}
[Webmethod]
[Scriptmethod (responseformat=Responseformat. XML)]
Public StringGetxmlstring ()
{
Return "<Employee> <Name> Jeffrey Zhao </Name> <salary> 1000 </salary> </employee>";
}
[Webmethod]
[Scriptmethod (responseformat = Responseformat. XML, xmlserializestring = True )]
Public String Getserializedstring ()
{
Return " <Employee> <Name> Jeffrey Zhao </Name> <salary> 1000 </salary> </employee> " ;
}
}