views:

271

answers:

4

Hey all,

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" />
+3  A: 

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;
}
rick schott
Excellent; I'll try this out. No way to set this globally, though, eh?
jvenema
OK, so that still returns the first half (the job= part), replacing "null" with "". Any other thoughts?
jvenema
Can you post your class definition that is being serialized?
rick schott
This setting has very little to do with the problem. All it does is switch between absent elements, and `xsi:nil` attribute, to indicate `null` values on properties _that are serialized as elements_. The question is quite specifically about properties serialized as attributes.
Pavel Minaev
A: 

From a schema perspective you are effectively saying that the elements are optional, this can be achieved ( at least according to the documentation ) through use of the DefaultValue attribute. See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmldefaultvalue.aspx

Neal
+2  A: 

The example xml is confusing, because in most cases the serializer will omit nulls, especially for attributes. The exception to this is Nullable<T> when used with elements, for example:

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

Might result in:

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

Which is again very different to your example xml. In the general case, it is possible to control serialization using a number of methods:

  • the IsNullable property of [XmlElement]
  • adding a [DefaultValue]
  • adding a public bool ShouldSerialize{propname}() {...} method
  • adding a [XmlIgnore] public bool {propname}Specified {get {...} set {...}} property

However; without a repeatable example to run your example against, it is impossible to answer fully.


Re the updated question; this should accomplish that, but it isn't clear what these other properties are (that you want to not show if 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
}
Marc Gravell
This was the most complete answer; as it turns out, there were a number of other factors causing all the nulls, not just the webservice (a JSON translation layer which anaylyzed what it "expected" to be there and inserted nulls). I'm going to give the credit here because this is, technically, correct. Thanks Marc.
jvenema
A: 

Are you sure

 GetColumnValue<string>("Username");

is returning null and not blank? This seems like the most likely explanation...

ck