在序列化期间控制 XmlType 元素名称
我有一个类,它是抽象类的列表
[XmlInclude(typeof(PostedPayment))]
[XmlInclude(typeof(PostedInvoice))]
[XmlType("VoucherProgress")]
public class PostedJournals : List<APostedJournal>
{
public PostedJournals(IEnumerable<APostedJournal> postedJournals) : base(postedJournals) { }
public PostedJournals() { }
}
,但是当我序列化时,我最终会得到
<VoucherProgress>
<APostedJournal p2:type="PostedPayment" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
...
</APostedJournal>
</VoucherProgress>
当我希望它命名类型,而不是使用抽象名称时,
<VoucherProgress>
<PostedPayment>
...
</PostedPayment>
</VoucherProgress>
有没有办法让它与某些属性一起工作?
I have a class that is a list of an abstract class
[XmlInclude(typeof(PostedPayment))]
[XmlInclude(typeof(PostedInvoice))]
[XmlType("VoucherProgress")]
public class PostedJournals : List<APostedJournal>
{
public PostedJournals(IEnumerable<APostedJournal> postedJournals) : base(postedJournals) { }
public PostedJournals() { }
}
But when I serialize, I end up with
<VoucherProgress>
<APostedJournal p2:type="PostedPayment" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
...
</APostedJournal>
</VoucherProgress>
When I want it to name the types, not use the abstract name
<VoucherProgress>
<PostedPayment>
...
</PostedPayment>
</VoucherProgress>
Is there a way to make this work with some attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,我已经告诉你了:(
我不知道如何将 XML 粘贴到这个该死的网站上。class
Program
{
静态无效主(字符串[]参数)
{
MyList tmp = new MyList();
该代码会生成您想要的列表,但带有其他 MtTypeBase 元素,您可以自行计算这些元素。它所做的一切都改变了基类的序列化方式。每个子类都会将 elType 更改为它自己的类类型,以便基类可以使用它来完成它的任务。我已经在这上面花了很多时间,所以你可以在这里了解它。有很多方法可以改进这一点并使其更好、更清洁。
Here, I got you this far:
(I have NO idea how to get XML pasted on this blasted site.
class Program
{
static void Main(string[] args)
{
MyList tmp = new MyList();
The code makes the list you want but with additional MtTypeBase elements which you can figure out on your own. All it does it changes the way the base class is serialized. Each subclass changes the elType to it's own class type so the base class can use it to do it's thing. I already spent a ton of time on this so you can take it form here. Plenty of ways to improve this and make it better and cleaner.