locked
How do I make a property invisible? RRS feed

  • Question

  • I have a web method that returns of type "Claim".

    public Claim GetClaims(string ClaimID)
    {
      return claim; 
    }


    Should I serialize the object and send XML to client consuming web service or
    return type "claim"? Which is the best method? Clients might further work on claim object like updating data.


    I used "XmlAttributeOverrides" at run time by setting XMLIgnore = true, but when I return the object "claim", clients still see the property but value is cleared. Is there any way I can hide a property?  Example if LastName is ignored, clients still can browse the lawname thought the value is empty.

    Webservice service = new localhost.Webservice();
    Claim claim = service.GetClaim("12345");

    I can still be able to browse "LastName" like claim.LastName. How can I hide it completely.

    PLEASE HELP. THANKS.

    Thursday, August 28, 2008 5:00 PM

Answers

  • You should just return the Claim. .NET will serialize it for you.

    If your property is not public, or doesn't have both a setter and a getter, then it will not be serialized. Otherwise, just do this:

    [XmlIgnore]  
    public string LastName  
    {  
        get {return _lastName;}  
        set {_lastName = value;}  
    }  
     
    // or in C# 3.0:  
    [XmlIgnore] public string FirstName {get;set;} 

    John Saunders | Use File->New Project to create Web Service Projects
    Thursday, August 28, 2008 5:36 PM
    Moderator
  • If it's a different set of properties, then it's a different type, and so, yes, you have to make it differ per-client.

    Remember that the client-side code will be generated based on what your WSDL says the properties are. If you include a property you don't want a client to see, their code will expect it, and it would then not be present. The WSDL is a contract. You may very well want a different contract for different clients, though that becomes difficult for you to manage.

    John Saunders | Use File->New Project to create Web Service Projects
    Thursday, August 28, 2008 8:05 PM
    Moderator

All replies

  • You should just return the Claim. .NET will serialize it for you.

    If your property is not public, or doesn't have both a setter and a getter, then it will not be serialized. Otherwise, just do this:

    [XmlIgnore]  
    public string LastName  
    {  
        get {return _lastName;}  
        set {_lastName = value;}  
    }  
     
    // or in C# 3.0:  
    [XmlIgnore] public string FirstName {get;set;} 

    John Saunders | Use File->New Project to create Web Service Projects
    Thursday, August 28, 2008 5:36 PM
    Moderator
  • Thanks for the reply.

    [XmlIgnore] works fine but that property might be availabe for some clients and for other clients it could be invisible.

    How can I achieve this? Should I make class "claim" client-specific? 
    Thursday, August 28, 2008 5:51 PM
  • If it's a different set of properties, then it's a different type, and so, yes, you have to make it differ per-client.

    Remember that the client-side code will be generated based on what your WSDL says the properties are. If you include a property you don't want a client to see, their code will expect it, and it would then not be present. The WSDL is a contract. You may very well want a different contract for different clients, though that becomes difficult for you to manage.

    John Saunders | Use File->New Project to create Web Service Projects
    Thursday, August 28, 2008 8:05 PM
    Moderator
  • Thank you so much for taking time to reply.
     
     
    Thursday, August 28, 2008 8:32 PM