6

我正在尝试以特定格式生成 xml 文档。我想跳过根据属性值对属性进行序列化。

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());

如果 Set 为 false,我希望不序列化整个属性,我生成的 xml 应该是

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

代替

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

有什么方法可以让我用 IXmlSerializable 或其他任何东西干净地做到这一点?

谢谢!

4

4 回答 4

6

有一个 ShouldSerialize* 模式(由 TypeDescriptor 引入,但被其他一些代码区域识别,例如 XmlSerializer):

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

那应该排序它。

但是,一个更简单的选择是将其分配为 null。

于 2011-06-18T23:31:04.027 回答
0

如果“mychild”是由数组定义的,我认为它可以做得很好......

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; }
}
于 2011-06-18T23:36:52.480 回答
0

我认为这可能有效,尽管您可能必须覆盖 Equals 方法

[DefaultValue(new Child())]
public Child myChild{ get; set; }
于 2011-06-19T01:57:22.697 回答
0

只是为了好玩而写了这段代码,也许在这个过程中学到了一些东西。如果该属性包含一个名为 Set 的返回 bool 的方法,并且其当前值为 false,则它应该将任何属性设置为 null。通过将值设置为 false,它应该可以解决序列化程序问题。有什么建议么:

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);
            }
        }
    }
}
于 2011-06-19T03:35:55.853 回答