对空 class 属性强制 xml 序列化?

Force xml serialization on empty class attribute?

我有一个嵌套 class:

public class guy{
    public address address {get; set;}
    public int age {get; set;}
}

public class address{
    public string name {get; set;}
}

有没有办法序列化一个没有地址名称的 guy 对象并用 XmlSerializer 得到下面的结果?

<guy>
   <address>
       <name></name>
   </address>
   <age>10</age>
</guy>

厌倦了在 name 字段上添加 [XmlElement(IsNullable = false)],但它似乎不起作用

尝试

public class guy
{
    [XmlElement(IsNullable = true)]
    public address address {get; set;}
    public int age {get; set;}
}

XmlElementAttribute.IsNullable

否则请确保在实例化 class

时初始化该字段
public class guy
{
    public address address { get; set; }
    public int age { get; set; }

    public guy()
    {
        address = new address();
    }
}