views:

51

answers:

1

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>
+3  A: 
[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;
}
AZ
AZ beat me to the answer
azheglov
Thanks - that will do it but any way to do without creating new class - just attributes maybe? Not that I want to moon on a stick or anything... ;)
Ryan
unfortunately there is no way to do that (as far a i know) without an additional class
AZ