Can asmx services return anything instead of zero?

I have a WebService that I support while working on .Net 2.0. It uses the original asmx file standard for a series of web services. These web services return some objects that potentially have 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 fact, we have a lot more “null” values. Since I really do not need zeros, because I can easily conclude that they are zero from their nonexistence, I would prefer not to return them at all. It can 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, I want to see:

<user id="1" name="foo" />
+3
4

xml , , . Nullable<T> , :

[XmlElement("job")]
public int? Job { get; set; }

, :

<user ...>
    <job xsi:nil="true" />
</user>

- xml. :

  • IsNullable [XmlElement]
  • [DefaultValue]
  • public bool ShouldSerialize{propname}() {...}
  • [XmlIgnore] public bool {propname}Specified {get {...} set {...}}

; , .


; , , ( , null).

[Serializable, XmlRoot("user")]
public partial class User
{
    [XmlAttribute("id")]
    public int Id {get;set;} // snipped more complex property implementation
    [XmlAttribute("name")]
    public string Username  {get;set;} // ditto
}
+2

XmlElementAttribute.IsNullable Property

IsNullable , XML- , (Nothing Visual Basic).

public class MyClass
{
   [XmlElement(IsNullable = false)]
   public string Group;
}
+3

From the point of view of the scheme, you are actually saying that the elements are optional, this can be achieved (at least according to the documentation) using the DefaultValue attribute. See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmldefaultvalue.aspx

0
source

You are sure that

 GetColumnValue<string>("Username"); 

returns null, not empty? This seems like the most likely explanation ...

0
source

Source: https://habr.com/ru/post/1719549/


All Articles