2012-07-31 14 views
5

XMLにオブジェクトをシリアル化しています。リストのXMLシリアル化

Class A 
{ 
    public string propertyA1 { get; set; } 
    public List<B> bList { get; set; } 
} 

Class B 
{ 
    public string num {get; set;} 
    public string propertyB1 { get; set; } 
} 

私はそれをXMLにシリアライズするとき、私はそれは次のようになりたい:私はこのような何か持って

<A> 
    <propertyA1>someVal</propertyA1> 
    <B num=1> 
    <propertyB1>someVal</propertyB1> 
    </B> 
    <B num=2> 
    <propertyB1>someVal</propertyB1> 
    </B> 
</A> 

をしかし、代わりに、それは次のようになります。

<A> 
    <propertyA1>someVal</propertyA1> 
    <bList> 
    <B num=1> 
     <propertyB1>someVal</propertyB1> 
    </B> 
    <B num=2> 
     <propertyB1>someVal</propertyB1> 
    </B> 
    </bList> 
</A> 

どのようにしてbListの出力を取り除くことができますか?

感謝を必要に応じて、私はより多くのサンプルコードを提供することができ、 スコット

答えて

15

要素のフラットリストとしてコレクションを治療するための属性[XmlElement]を追加します。詳細は

Class A 
{ 
    public string propertyA1 { get; set; } 
    [XmlElement("B")] 
    public List<B> bList { get; set; } 
} 

はクリックhere

+0

、優れたシンプルでわかりやすい - 使用しようとしていた[この](http://stackoverflow.com/questions/1237683/xml-serialization-of-listt-xml-root)あなたのソリューションははるかに簡単です。 – Iztoksson