开发者

Xml Serialization Dynamic Ignore

开发者 https://www.devze.com 2023-03-14 02:12 出处:网络
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.

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.开发者_Go百科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!


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.


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; }
}


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

[DefaultValue(new Child())]
public Child myChild{ get; set; }


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);
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消