Rick Kierner posted on July 17, 2008 11:50

If you're dealing with an ASP.NET web service and a web front end that is using ajax, it may be too much trouble to attempt to parse the XML.  XML can be clunky and contain way more information that you need.  It's pretty simple to change the output of a web service method to return JSON:

   1:  [WebService(Namespace = "http://tempuri.org/")]
   2:  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   3:  public class Service : System.Web.Services.WebService
   4:  {
   5:      public Service () {}
   6:   
   7:      [WebMethod]
   8:      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
   9:      public MyObject HelloWorld(){return new MyObject();}
  10:  }
  11:   
  12:  public class MyObject
  13:  {
  14:      public MyObject()
  15:      {
  16:          MyString = "Test";
  17:          MyInt = 10;
  18:      }
  19:   
  20:      public string MyString;
  21:      public int MyInt;
  22:  }

Note Line 8.

But Rick,  JSON is harder to parse with my .Net class library than xml is.  Well, I don't know that I would necessarily recommend this approach for a .Net to .Net type of communication.  But let's just imagine that you want to do real ajax.  I'm not talking the ASP.NET ajax.  Now let's leverage a JavaScript library like JQuery.  JQuery can parse that JSON format right into a JavaScript object.  Just awesome.  I may post some more on this in the future.  I just thought the ResponseFormat property was kickin.

Technorati Tags: ,,,

Posted in:   Tags: , , ,

Comments

Comments are closed
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Rick.Brain.Flush()