WebService returns the JSON format and returns the data in XML format

Source: Internet
Author: User

Return JSON format

//using System.Web.Script.Services;[WebMethod] [Scriptmethod (Usehttpget=true, Responseformat =Responseformat.json)] Public voidHelloWorld () {Context.Response.Clear (); Context.Response.ContentType="Application/json"; Model.User.User_User User=NewModel.User.User_User (); User. UserName="we are"; User. UID=1; User. UserPassword="123456"; Jayrock.Json.JsonTextWriter writer=NewJayrock.Json.JsonTextWriter ();            Jayrock.Json.Conversion.JsonConvert.Export (user, writer); Context.Response.Write (writer.        ToString ()); }

Effect:

return XML format

[WebMethod] PublicXmlDocument HelloWorld () {downlist doo=Newdownlist (); List<file> flist =NewList<file>(); File F=Newfile (); F.name="Test"; F.value="T"; Flist.            ADD (f); List<sql> slist =NewList<sql>(); List<desc> dlist =NewList<desc>(); Version ver=Newversion (); Doo.version=ver; Doo.sqllist=slist; Doo.filelist=flist; XmlDocument xmldoc=NewXmlDocument (); stringXMLString =Utility.Tool.Serialize (doo);            Xmldoc.loadxml (xmlstring); returnxmldoc; }
 Public classdownlist { PublicList<file>fileList;  PublicList<sql>sqllist;  Publicversion version;  PublicList<desc>desclist;  Public intresult; }     Public classSQL {[XmlText] Public stringvalue; } [Serializable] Public classfile {[XmlAttribute] Public stringname; [XmlText] Public stringvalue; }     Public classdesc {[XmlText] Public stringvalue; } [Serializable] Public classversion {[XmlAttribute] Public stringname; }

/// <summary>        ///serializes the specified object into an XML-formatted string and returns. /// </summary>        /// <param name= "O" >objects to serialize</param>        /// <returns>returns the serialized string</returns>         Public Static stringSerialize (Object o) {stringXML =""; Try{XmlSerializer Serializer=NewXmlSerializer (O.gettype ()); using(MemoryStream mem =NewMemoryStream ()) {                    using(XmlTextWriter writer =NewXmlTextWriter (Mem, Encoding.UTF8)) {writer. Formatting=formatting.indented; XmlSerializerNamespaces N=Newxmlserializernamespaces (); N.add ("",""); Serializer.                        Serialize (writer, o, N); Mem. Seek (0, Seekorigin.begin); using(StreamReader reader =NewStreamReader (MEM)) {XML=Reader.                        ReadToEnd (); }                    }                }            }            Catch{XML =""; } returnXML; }

Effect:

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.