可能重复:
Removing Wrapper Elements from XML-Serialized Array不需要的属性名称用XML序列化
这是难以解释的,所以我包括我的问题的一个例子。我有一个包含子类的列表的父类。当我序列化父项时,我得到我的子类,但是它们在具有公共属性名称的元素下。额外的水平不是我所需要的。我尝试将XmlIgnore属性添加到属性名称,但是它压缩了所有属性名称和它包含的发票集合。
父类:
[XmlRoot("header")]
public class Lynx : INotifyPropertyChanged
{
#region /*-- Class Fields --*/
private List<InvoiceItem> _invoice = new List<InvoiceItem>();
#endregion
[XmlArray("invoice")]
[XmlArrayItem("invoice", typeof(InvoiceItem))]
public List<InvoiceItem> invoice
{
get
{
return _invoice;
}
set
{
if (value != _invoice)
{
_invoice = value;
OnPropertyChanged("invoice");
}
}
}
子类:
[XmlType(TypeName = "invoice")]
public class InvoiceItem : INotifyPropertyChanged
{
... properties and methods of the class
}
这是它正在建设:
<header>
<headerid>790aa61a-ad1b-49b9-bfb9-01fe3ca55eca</headerid>
<invoice> <-- this line is not needed
<invoice>
<company>BRU111</company>
<format>myformat</format>
...
这是我需要建立:
<header>
<headerid>790aa61a-ad1b-49b9-bfb9-01fe3ca55eca</headerid>
<invoice>
<company>BRU111</company>
<format>myformat</format>
...
(删除我的答案@Tuzo发现了一个很好的现有重复,但:使用'[XmlElement(“invoice”)]''而不是您当前的属性) –
@Tuzo - 良好的发现和感谢,我无法表达提问足以找到另一个。 – jac