列表的XML序列化

Doo*_*Dah 6 .net c# xml xml-serialization

我正在将对象序列化为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; }
}
Run Code Online (Sandbox Code Playgroud)

当我将它序列化为XML时,我希望它看起来像这样:

<A>
  <propertyA1>someVal</propertyA1> 
  <B num=1>
     <propertyB1>someVal</propertyB1> 
  </B>
  <B num=2>
     <propertyB1>someVal</propertyB1> 
  </B>
</A>
Run Code Online (Sandbox Code Playgroud)

但是,它看起来像这样:

<A>
  <propertyA1>someVal</propertyA1> 
  <bList>
     <B num=1>
        <propertyB1>someVal</propertyB1> 
     </B>
     <B num=2>
        <propertyB1>someVal</propertyB1> 
     </B>
  </bList>
</A>
Run Code Online (Sandbox Code Playgroud)

知道怎么摆脱输出中的bList吗?如果需要,我可以提供更多示例代码

谢谢,斯科特

D S*_*ley 16

添加属性[XmlElement]以将集合视为元素的平面列表:

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

欲了解更多信息,请点击