.NET序列化:最佳实践映射类的元素和属性的属性? [英] .NET serialization: Best practice to map classes to elements and properties to attributes?

查看:114
本文介绍了.NET序列化:最佳实践映射类的元素和属性的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化以下对象:

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
}

试图实施的对象的元素和属性的属性的一种策略,但你可能会注意到,结果显示我不太有,因为编译器是理所当然preventing我设置了 [的XmlElement(书)] 属性添加到图书类。

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

我已经考虑创建一个假设名单,其中,图书&GT; 具有明显的想法对象中的&LT;图书&GT; 根元素序列化,但是,一方面,我不是很确定,如果这样做实际工作,并在另一方面,它很可能使序列排序依赖于实际执行的。

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.

我真的AP preciate,你们可以考虑可能是有帮助的,包括系列化战略的改变任何意见,等等。

I'd really appreciate any advice that you guys might consider could be helpful, including a change of serialization strategy, etcétera.

感谢很多提前,

推荐答案

您可以(例如)使用一个容器对象,使之琐碎控制书。

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;}}
}

另一种选择(对于两个级别)是XmlArray / XmlArrayItem。

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

这篇关于.NET序列化:最佳实践映射类的元素和属性的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆