简体   繁体   中英

C# XmlElement Twice the same element

I am currently developing a module to generate an XML file.

As a graduate at the instantiation of a list, my tag appears twice at the beginning (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>

We have the list named operation_unitaires which normally contains the object operation_unitaire

MODEL 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()
    {

    }
}

MODEL operations_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; }

[...]

Has anyone ever had the problem ?

UPDATE 1

If I try to rename the name of the object , operation_unitaire to 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

and i have this result

 <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 />

[...]

So How to stop this tag (operation_unitairess) ?

The tag is not appearing twice.

<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>
        <...>

"We have the list named operation_unitaires" (this is not the name of your list).

[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

then name of your list is 'operation_unitaire'.

It was just a hierarchical problem

Today i have this

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

Who call this model :

[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

Who call object

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

So it's logical, just call the object list directly from the first model !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM