c# - 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
解决方案
将输入 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)]
在你的名单上-礼节
推荐阅读
- javascript - How to add customize tooltip on Google Chart?
- docker - 是否有最大数量的容器可以同时连接到网络?
- node.js - Nodejs流链接
- javascript - 编写测试用例时,在 afterAll() 函数中不执行 SQL 查询
- java - 在 JPanel 上将 JLabel 向左或向右对齐
- mysql - 不是有效的 BCrypt 哈希。发生错误
- mysql - SQL 脚本无法创建某些表
- c# - 使用跨度
作为子字符串的替代品 - io - 将列表写入 lisp 中的文件
- c# - 如何仅在 C# 中获取 RSACryptoServiceProvider 公钥和私钥