C#XmlElement两次相同的元素

时间:2017-05-24 07:43:52

标签: c# xml attributes

我目前正在开发一个模块来生成XML文件。

作为名单实例化的毕业生,我的标签在开头出现两次(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

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

[...]

有没有人遇到过这个问题?

更新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)?

2 个答案:

答案 0 :(得分:0)

标签没有出现两次。

let intStruct   = MyStruct(someNum: 4)
let floatStruct = MyStruct(someNum: 5.0)

“我们有名为operation_unitaires的列表” (这不是列表的名称)。

<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_unitaire'。

答案 1 :(得分: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; }

所以这是合乎逻辑的,只需直接从第一个模型调用对象列表!