Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can asmx services return nothing instead of null?

I have a WebService I'm maintaining, running on .Net 2.0. It uses the original "asmx" file standard for a series of web services. In these web services, some objects are returned that have potentially a large number of "null" values. For example:

<user id="1" name="foo" job="null" location="null" audience="null" />

This is a simple example; in reality, we have a lot more "null" values. Since I don't really need to have the nulls because I can easily infer that they're null from their non-existence, I'd prefer to not return them at all. Can this be done? If so, how?

Edited to add class definition:

[Serializable]
public partial class User

    [XmlAttribute("Id")]
    public int Id 
    {
        get { return GetColumnValue<int>("ID"); }

        set { SetColumnValue("ID", value); }

    }



    [XmlAttribute("Username")]
    public string Username 
    {
        get { return GetColumnValue<string>("Username"); }

        set { SetColumnValue("Username", value); }

    }
}

By the way, what I'm aiming to see is:

<user id="1" name="foo" />
like image 678
jvenema Avatar asked Oct 19 '25 12:10

jvenema


1 Answers

XmlElementAttribute.IsNullable Property

If the IsNullable property is false, no XML element is generated for class members that have been set to a null reference (Nothing in Visual Basic).

public class MyClass
{
   [XmlElement(IsNullable = false)]
   public string Group;
}
like image 165
rick schott Avatar answered Oct 22 '25 02:10

rick schott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!