首页 > 解决方案 > XML De/Serialization : How to define different name for same property of different instance of the class

问题描述

I'm trying to serialize an object which contains two lists of the same object.

public class Foo
{
   public List<ClassA> DataA {get;set;}
   public List<ClassA> DataB {get;set;}
}

public class ClassA
{
  public string Code {get;set;}
  public string Value {get;set;}
}

In the xml file I would like that the properties of the class have different name. Something like this :

<Foo>
  <DataA>
    <ClassA>
      <Code></Code>
      <Value></Value>
    </ClassA>
  </DataA>
  <DataB>
    <ClassA>
      <Name></Name>
      <Text></Text>
    </ClassA>
  </DataB>
</Foo>

I tried to use "XmlAttributeOverrides" but the property name is modified for all instance, not just one.

Is there a way to do this ? Maybe a custom XmlSerializer ?

Thanks

标签: c#xmlserialization

解决方案


将输入 XML 复制到剪贴板

<Foo>
  <DataA>
    <ClassA>
      <Code></Code>
      <Value></Value>
    </ClassA>
    <ClassA>
      <Code></Code>
      <Value></Value>
    </ClassA>
  </DataA>
  <DataB>
    <ClassA>
      <Name></Name>
      <Text></Text>
    </ClassA>
    <ClassA>
        <Code></Code>
        <Value></Value>
    </ClassA>
  </DataB>
</Foo>

并在 Visual Studio 中向您的项目添加一个新的类文件。选择并清除命名空间内新创建文件的所有内容 an use Edit->paste Special->insert XML as code(或类似的东西,我有一个德语 IDE)

在此处输入图像描述

应该给你这样的东西(经过一些重组)

namespace SandBoxApp
{

  // HINWEIS: Für den generierten Code ist möglicherweise mindestens .NET Framework 4.5 oder .NET Core/Standard 2.0 erforderlich.
  /// <remarks/>
  [System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
  public partial class Foo
  {

    private FooClassA[] dataAField;

    private FooClassA[] dataBField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("ClassA", IsNullable = false)]
    public FooClassA[] DataA
    {
      get
      {
        return this.dataAField;
      }
      set
      {
        this.dataAField = value;
      }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("ClassA", IsNullable = false)]
    public FooClassA[] DataB
    {
      get
      {
        return this.dataBField;
      }
      set
      {
        this.dataBField = value;
      }
    }
  }

  /// <remarks/>
  [System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  public partial class FooClassA
  {

    private object codeField;

    private object valueField;

    /// <remarks/>
    public object Code
    {
      get
      {
        return this.codeField;
      }
      set
      {
        this.codeField = value;
      }
    }

    /// <remarks/>
    public object Value
    {
      get
      {
        return this.valueField;
      }
      set
      {
        this.valueField = value;
      }
    }
  }

您可能希望根据您的需求进行重构,但它应该为您指明方向。

总结一下,我认为你错过了属性:

 [System.Xml.Serialization.XmlArrayItemAttribute("ClassA", IsNullable = false)]

在你的名单上-礼节


推荐阅读