Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml serialization specify xmlelement and xmlattribute together

Given :-

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

   [XmlElement]
   public string Publisher;

   [XmlElement]
   public string PublisherReference;
}

When serialized to XML will give

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

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

<Book Title="My Book">
   <Publisher Reference="XYZ123">Some Publisher</Publisher>
</Book>
like image 812
Ryan Avatar asked Sep 29 '10 16:09

Ryan


1 Answers

[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;
}
like image 68
Adrian Zanescu Avatar answered Oct 06 '22 00:10

Adrian Zanescu