我想知道在序列化以下类时是否可以更改通用列表的元素名称。
[Serializable, XmlRoot(ElementName= "MyFoo")]
public class Foo : List<Foo2>
{
#region Private Members
#endregion
#region Public Properties
#endregion
}
[Serializable, XmlRoot(ElementName="YourFoo")]
public class Foo2
{
}
The output is:
<MyFoo>
<Foo2/>
</MyFoo>
The desire output:
<MyFoo>
<YourFoo/>
</MyFoo>
最佳答案
只需将 Foo2
上的 XmlRoot
更改为 XmlType
并将 ElementName
更改为 TypeName
,像这样:
[Serializable, XmlType(TypeName="YourFoo")]
public class Foo2
{
}
关于c# - 具有动态元素名称的序列化和通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9298360/