2017-05-24 60 views
0

我目前正在開發一個模塊來生成一個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)?

+1

您列表的名稱與對象相同,請儘量讓它們保持不同,您還應該閱讀C#編碼風格。 –

+0

我正在嘗試一些測試 –

回答

0

這僅僅是一個層次的問題

今天我有這個

[XmlElement(ElementName = "operations_unitaires", Order = 3, IsNullable = false, Type = typeof(operations_unitaires))] 
    [DataMember(Name = "operations_unitaires", Order = 3, IsRequired = false)] 
    public operations_unitaires operations_unitaires { get; set; } 

誰叫這個模型:

[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_unitairess { get; set; } //0..n 

誰叫對象

[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; } 

所以它的邏輯,只是打電話該對象列表直接來自第一個模型!

0

該標籤沒有出現兩次。

<compte_type>DROITS_SUSPENDUS</compte_type> 
    <stock_epuise>0</stock_epuise> 
    <operations_unitaires> <!-- your class operations_unitaires --> 
    <operation_unitaire> <!-- your collection of operation_unitaire --> 
     <operation_unitaire> <!-- an instance of of operation_unitaire --> 
     <categorie_operation>SORTIE</categorie_operation> 
     <...> 

「我們有名單所列operation_unitaires」 (這不是你的列表的名稱)。

[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 

那麼你的名單是'operation_unitaire'。

+0

謝謝,我明白你的意思。那麼如何讓這個標籤不出現?也許[XmlIgnore]? –

+0

我不會嘗試對標籤進行任何形式的操作 - 儘可能使描述代碼的XML儘可能靠近。如果你真的需要更改XML,那麼你可能會考慮[實現IXmlSerializable](https://stackoverflow.com/questions/279534/proper-way-to-implement-ixmlserializable),但這有點高級。 – Ben

相關問題