2

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,

Nano Taboada
  • 4,148
  • 11
  • 61
  • 89
  • Btw; you ask about overall strategy - is XML a requirement? Ther – Marc Gravell Oct 23 '10 at 07:39
  • Hey Marc, thanks much for taking time to reply. XML is currently a requirement and attributes was the way to go back then given that such XML 'digest' is copious -- At some point I've considered not 'hinting' the serializer hence letting it do 'regular' serialization to elements then doing some file compression but refactoring the parsing on the receiving end is not an option. – Nano Taboada Oct 24 '10 at 00:03

1 Answers1

3

You can (for example) use a container object to make it trivial to control "books".

[XmlRoot("books"), XmlType("books")]
public class Library
{
    private readonly List<Book> books;
    [XmlElement("book")]
    public List<Book> Books {get{return books;}}
}

Another option (for two levels) is XmlArray/XmlArrayItem.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks Marc! I've barely modified the Library class to include the creation of the list: private List books = new List(); Now it renders the results I've been expecting. Cheers! – Nano Taboada Oct 24 '10 at 20:01