2012-05-24 57 views

回答

6

你的意思是你想這樣的:

<parent> 
    <child1>Hello World</child1> 
    <child2 /> 
</parent> 

,而不是

<parent> 
    <child1>Hello World</child1> 
</parent> 

類應該是這樣的: 串行器調用由定義一個ShouldSerializePropertyName方法(如果存在的話),以確定是否屬性應該被序列化(就像Windows窗體設計器一樣)。

public class Parent 
{ 
    [XmlElement("Child1")] 
    public string Child1 { get; set; } 

    [XmlElement("Child2")] 
    public string Child2 { get; set; } 

    public bool ShouldSerializeChild2() { return true; } 

}