6

I am trying to generate an xml document in a specific format. I would like to skip serializing a property depending on a value of the property.

public class Parent
{
    public Parent()
    {
        myChild = new Child();
        myChild2 = new Child() { Value = "Value" };
    }
    public Child myChild { get; set; }
    public Child myChild2 { get; set; }
}

public class Child
{
    private bool _set;
    public bool Set { get { return _set; } }

    private string _value = "default";
    [System.Xml.Serialization.XmlText()]
    public string Value
    {
        get { return _value; }
        set { _value = value; _set = true; }
    }
}

System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Parent));
x.Serialize(Console.Out, new Parent());

If Set is false, I want the entire property to not be serialized, my resulting xml should be

<Parent>
   <myChild2>default</myChild2>
</Parent>

Instead of

<Parent>
   <myChild/>
   <myChild2>default</myChild2>
</Parent>

Is there some way I can do this cleanly with IXmlSerializable or anything else?

Thanks!

TrevDev
  • 1,106
  • 11
  • 24

4 Answers4

7

There is a ShouldSerialize* pattern (introduced by TypeDescriptor, but recognised by a few other areas of code, such as XmlSerializer):

public bool ShouldSerializemyChild() {
     return myChild != null && myChild.Set;
}

That should sort it.

A simpler option, though, is to assign it null.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I have a lot of properties that are all of type Child, I would prefer not to have the ShouldSerialize for every single one. I might set all my properties to null that aren't set before I serialize.. – TrevDev Jun 18 '11 at 23:50
  • I posted an answer for dynamically setting the value to null via a method that would be called before serializing. Did you have a cleaner idea for assigning a null value? – TrevDev Jun 19 '11 at 03:36
0

If "mychild" is defined by the array , i think it can do well...

public class Parent
{
    public Parent()
    {
        myChild = new Child[]{ new Child(){Value = "Value"}};
        //myChild2 = new Child() { Value = "Value" };
    }
    public Child[] myChild { get; set; }
    //public Child myChild2 { get; set; }
}
svick
  • 236,525
  • 50
  • 385
  • 514
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
0

I think this could work, though you migh have to overide the Equals method

[DefaultValue(new Child())]
public Child myChild{ get; set; }
Martin Brenden
  • 257
  • 2
  • 8
0

Just wrote this code for fun and maybe learn something in the process. It should set any property to null if that property contains a method called Set that returns bool, and its current value is false. By setting the values to false, it should solve the serializer issue. Any suggestions:

public static void RemoveUnsetObjects(object currentObject)
{
    var type = currentObject.GetType();
    if (currentObject is IEnumerable)
    {
        IEnumerable list = (currentObject as IEnumerable);
        foreach (object o in list)
        {
            RemoveUnsetObjects(o);
        }
    }
    else
    {
        foreach (var p in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
        {
            var propertyValue = p.GetValue(currentObject, null);
            if (propertyValue == null)
                continue;
                    var setPropInfo = p.PropertyType.GetProperty("Set", typeof(bool));
            if (setPropInfo != null)
            {
                var isSet = (bool)setPropInfo.GetValue(propertyValue, null);
                if (!isSet)
                {
                    p.SetValue(currentObject, null, null);
                }
            }
            else
            {
                RemoveUnsetObjects(propertyValue);
            }
        }
    }
}
TrevDev
  • 1,106
  • 11
  • 24