我目前正在開發一個模塊來生成一個XML文件。C#XmlElement兩次相同的元素
截至列表的實例化一個研究生,我的標籤在開始出現兩次(operation_unitaire)
<compte_type>DROITS_SUSPENDUS</compte_type>
<stock_epuise>0</stock_epuise>
<operations_unitaires>
<operation_unitaire>
<operation_unitaire>
<categorie_operation>SORTIE</categorie_operation>
<date_operation>2016-02-24T00:00:00</date_operation>
<produit>
<code_bivb />
<millesime>0</millesime>
<degre_alcoolique>0</degre_alcoolique>
</produit>
<type_operation />
<type_justificatif />
<reference_justificatif>HB024227</reference_justificatif>
<conditionnement />
<volume>900.00000</volume>
</operation_unitaire>
<operation_unitaire>
<categorie_operation>SORTIE</categorie_operation>
<date_operation>2016-02-23T00:00:00</date_operation>
<produit>
<code_bivb />
<millesime>0</millesime>
<degre_alcoolique>0</degre_alcoolique>
</produit>
<type_operation />
<type_justificatif />
<reference_justificatif>HB024200</reference_justificatif>
<conditionnement />
<volume>900.00000</volume>
</operation_unitaire>
</operation_unitaire>
</operations_unitaires>
我們名單所列operation_unitaires通常包含operation_unitaire
模型operations_unitaires對象
[DataContract]
public class operations_unitaires : ISerializable
{
[XmlElement(ElementName = "operation_unitaire", Order = 1, IsNullable = true, Type = typeof(List<operation_unitaire>))]
[DataMember(Name = "operation_unitaire", Order = 1, IsRequired = false)]
public List<operation_unitaire> operation_unitaire { get; set; } //0..n
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
public operations_unitaires()
{
}
}
模型化經營ns_unitaire
[DataContract]
public class operation_unitaire : ISerializable
{
[XmlElement(ElementName = "categorie_operation", Order = 1, IsNullable = false, Type = typeof(string))]
[DataMember(Name = "categorie_operation", Order = 1, IsRequired = true)]
public string categorie_operation { get; set; }
[XmlElement(ElementName = "date_operation", Order = 2, IsNullable = false, Type = typeof(DateTime))]
[DataMember(Name = "date_operation", Order = 2, IsRequired = true)]
public DateTime date_operation { get; set; }
[...]
有沒有人有問題?
更新1
如果我嘗試重命名對象的名稱,operation_unitaire到operation_unitairess
[DataContract]
public class operations_unitaires : ISerializable
{
[XmlElement(ElementName = "operation_unitairess", Order = 1, IsNullable = true, Type = typeof(List<operation_unitaire>))]
[DataMember(Name = "operation_unitairess", Order = 1, IsRequired = false)]
public List<operation_unitaire> Operation_unitaire { get; set; } //0..n
,我有這樣的結果
<compte_type>DROITS_SUSPENDUS</compte_type>
<stock_epuise>0</stock_epuise>
<operations_unitaires>
<operation_unitairess>
<operation_unitaire>
<categorie_operation>SORTIE</categorie_operation>
<date_operation>2016-02-24T00:00:00</date_operation>
<produit>
<code_bivb />
[...]
那麼如何阻止這個標記(operation_unitairess)?
您列表的名稱與對象相同,請儘量讓它們保持不同,您還應該閱讀C#編碼風格。 –
我正在嘗試一些測試 –