C# .NET序列化:将类映射到元素,将属性映射到属性的最佳实践?

C# .NET序列化:将类映射到元素,将属性映射到属性的最佳实践?,c#,.net,xml-serialization,C#,.net,Xml Serialization,我正在尝试序列化以下对象: [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")] publi

我正在尝试序列化以下对象:

[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
}
试图实现一种从对象到元素、从属性到属性的策略,但正如您可能注意到的那样,结果显示我还不太清楚,因为编译器正合理地阻止我将
[xmlement(“book”)]
属性设置为
book

<?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"
/>

我考虑过创建一个
列表
对象,其明显的想法是让
根元素进行序列化,但一方面我不太确定这是否真的有效,另一方面它可能会使序列化在某种程度上依赖于实际实现

我真的很感激你们可能认为有帮助的建议,包括改变序列化战略等。 提前多谢,

您可以(例如)使用容器对象使控制“书籍”变得简单

[XmlRoot(“books”)、XmlType(“books”)]
公共班级图书馆
{
私人只读书目;
[XmlElement(“书”)]
公共列表图书{获取{归还图书;}
}

另一个选项(用于两个级别)是XmlArray/XmlArrayItem.

Btw;您询问总体策略—XML是一种需求吗?TherHey Marc,非常感谢您抽出时间回复。XML目前是一种需求,鉴于XML“摘要”非常丰富,属性是一种回溯的方式——在某种程度上,我考虑过不“暗示”序列化程序,因此让它对元素进行“常规”序列化,然后进行一些文件压缩,但在接收端重构解析不是一种选择。谢谢Marc!我几乎没有修改Library类来包含list的创建:private list books=new list();现在它呈现了我一直期待的结果。干杯
[XmlRoot("books"), XmlType("books")]
public class Library
{
    private readonly List<Book> books;
    [XmlElement("book")]
    public List<Book> Books {get{return books;}}
}