Monday, November 2, 2009

Code: C# Hide Properties When Returning to Web Service

The automatic serialization built into the .Net Framework makes life easy when creating web services because it allows you to just return an object (or list of objects) and the framework will handle generating the XML return structure.  In the case where you have a property on an object that you don’t want returned in your web service method, you can add an an XmlIgnoreAttribute attribute to the property.

Code Snippet
  1. public class ReportInfo
  2. {
  3.     public string Title;
  4.     public decimal p95;
  5.     public DateParts DateBreakDown;
  6.     public List<ReportBase> Data;
  7.     //don't show this in web services
  8.     [System.Xml.Serialization.XmlIgnoreAttribute]
  9.     public List<TranscodingReportData> TranscodingData;
  10. }