Let the method return XML Object

Source: Internet
Author: User
• 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> " ;
}

}

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.