【问题标题】:XML attribute change repetitions class nameXML 属性更改重复类名
【发布时间】:2019-09-13 18:24:52
【问题描述】:

我有两个从实体框架导入的 XML 可序列化类,父子类。总结成这样

[Table("OecPreorden")]
[DataContract]
public partial class OecPreorden
{
    public OecPreorden()
    {
        OecPreordenProductos = new HashSet<OecPreordenProductos>();
    }

    [DataMember(IsRequired = false, Order = 1, Name = "ProductosComerciales")]
    public virtual ICollection<OecPreordenProductos> OecPreordenProductos { get; set; }
}


[Table("OecPreordenProductos")]
[DataContract]
public class OecPreordenProductos
{
    public OecPreordenProductos()
    {

    }
    [XmlIgnore()]
    public int Id { get; set; }

    [DataMember]
    [Required]
    public long IdPromocion { get; set; }

    [DataMember]
    [Required]
    public long IdProductoComercial { get; set; }

    [DataMember]
    [Required]
    public long NroOrden { get; set; }
    public int PreOrden_id { get; set; }
    public virtual OecPreorden OecPreorden { get; set; }
}

当我看到 XML 时,它是这样显示的


<dir:ProductosComerciales>
  <!--Zero or more repetitions:-->
    <dir:OecPreordenProductos>
    <dir:IdPromocion>?</dir:IdPromocion>
    <dir:IdProductoComercial>?</dir:IdProductoComercial>
    <dir:NroOrden>?</dir:NroOrden>
  </dir:OecPreordenProductos>
</dir:ProductosComerciales>

因为它有零个或多个重复......它显示正确的标题与

<dir:ProductosComerciales>

我要改变的是迭代的标题..

<dir:OecPreordenProductos>

当我设置时

[XmlRoot("AAA")]
public class OecPreordenProductos

[XmlType(TypeName = "AAA")]
public class OecPreordenProductos

它不起作用,它仍然显示类名。

如何更改重复类的名称?

【问题讨论】:

  • 您必须使用属性上方的 [XmlElement("AAA")] 在父级中更改它。
  • 它不起作用..谢谢

标签: c# xml xml-serialization


【解决方案1】:

您是否尝试过在 OecPreordenProductos-Property 上设置 [XmlArrayItem("AAA")]

    [Table("OecPreorden")]
        [DataContract]
        public partial class OecPreorden
        {
            public OecPreorden()
            {
                OecPreordenProductos = new HashSet<OecPreordenProductos>();
            }
           [DataMember(IsRequired = false, Order = 1, Name = "ProductosComerciales")]
           [XmlArrayItem("AAA")]
            public virtual ICollection<OecPreordenProductos> OecPreordenProductos { get; set; }
      }

【讨论】:

  • 我用 [XmlArrayItem("AAA")] 进行了测试,但它不起作用。谢谢
【解决方案2】:

应该是:

[DataMember(IsRequired = false, Order = 1, Name = "ProductosComerciales")]
[XmlArray("OecPreordenProductos")]
[XmlArrayItem("AAA)]
public virtual ICollection<OecPreordenProductos> OecPreordenProductos { get; set; }

【讨论】:

  • 标签名称是什么?检查 XML 文件的日期以确保日期与运行代码相同。更改应该会起作用。
猜你喜欢
  • 2015-08-10
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
相关资源
最近更新 更多