本文介绍了xml序列化一起指定xmlelement和xmlattribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出:-

[XmlRoot("Book")]
public class Book
{
   [XmlAttribute]
   public string Title;

   [XmlElement]
   public string Publisher;

   [XmlElement]
   public string PublisherReference;
}

序列化为XML时会给出

When serialized to XML will give

<Book Title="My Book">
   <Publisher>Some Publisher</Publisher>
   <PublisherReference>XYZ123</PublisherReference>
</Book>

如何获取PublisherReference作为Publisher的属性-例如

How could I get PublisherReference as an attribute of Publisher - e.g.

<Book Title="My Book">
   <Publisher Reference="XYZ123">Some Publisher</Publisher>
</Book>

推荐答案

[XmlRoot("Book")]
public class Book
{
   [XmlAttribute]
   public string Title;

   [XmlElement]
   public Publisher Publisher;
}

[Serializable]
public class Publisher
{
  [XmlText]
  public string Value;

  [XmlAttribute]
  public string Reference;
}

这篇关于xml序列化一起指定xmlelement和xmlattribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:35