I'm trying to serialize the following object:
[XmlRoot("book")]
public class Book
{
#region Properties
[XmlAttribute("isbn-10")]
public string Isbn10 { get; set; }
[XmlAttribute("isbn-13")]
public string Isbn13 { get; set; }
[XmlAttribute("title")]
public string Title { get; set; }
[XmlAttribute("author")]
public string Author { get; set; }
[XmlAttribute("collaborator")]
public string Collaborator { get; set; }
[XmlAttribute("publisher")]
public string Publisher { get; set; }
[XmlAttribute("publication")]
public DateTime? Publication { get; set; }
[XmlAttribute("pages")]
public int Pages { get; set; }
[XmlAttribute("instock")]
public bool InStock { get; set; }
#endregion
#region Constructors
public Book (string isbn10, string title, string author, string publisher, DateTime publication, int pages, bool instock=true, string isbn13="N/A", string collaborator="N/A")
{
this.Isbn10 = isbn10;
this.Isbn13 = isbn13;
this.Author = author;
this.Collaborator = collaborator;
this.Publisher = publisher;
this.Publication = publication;
this.Pages = pages;
this.InStock = instock;
}
public Book ()
{
// To be serialized by an XmlSerializer object, a class must have a default constructor.
// For additional information about XML Serialization Considerations, visit the following
// Microsoft Web site: http://msdn2.microsoft.com/en-us/library/182eeyhh(vs.71).aspx
}
#endregion
}
Trying to implement a objects to elements and properties to attributes kind of strategy, but as you probably might notice, results are showing I'm not quite there as the compiler is rightfully preventing me to set the [XmlElement("book")]
attribute to the Book
class.
<?xml version="1.0" encoding="utf-16"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
isbn-10="1577315936"
isbn-13="N/A"
author="Joseph Campbell"
collaborator="N/A"
publisher="New World Library"
publication="2008-07-28T00:00:00"
pages="432"
instock="true"
/>
I've considered creating a let's say List<Book>
object with the obvious idea of having a <books >
root element to serialize but on the one hand I'm not quite sure if that would actually work and on the other hand it would probably make the serialization sort of dependent of the actual implementation.
I'd really appreciate any advice that you guys might consider could be helpful, including a change of serialization strategy, etcétera.
Thanks much in advance,