条件xml序列化

时间:2010-11-04 08:43:27

标签: c# xml xml-serialization

我有以下C#类:

public class Books
{

public List<Book> BookList;

}

public class Book
{

public string Title;
public string Description;
public string Author;
public string Publisher;

}

如何将此类序列化为以下XML?

<Books>
  <Book Title="t1" Description="d1"/>
  <Book Description="d2" Author="a2"/>
  <Book Title="t3" Author="a3" Publisher="p3"/>
</Books>

我希望XML只包含那些值为null / empty的属性。 例如:在第一个Book元素中,author是空白的,因此它不应出现在序列化的XML中。

4 个答案:

答案 0 :(得分:34)

您应该可以使用ShouldSerialize*模式:

public class Book
{
    [XmlAttribute] 
    public string Title {get;set;}

    public bool ShouldSerializeTitle() {
        return !string.IsNullOrEmpty(Title);
    }

    [XmlAttribute]
    public string Description {get;set;}

    public bool ShouldSerializeDescription() {
        return !string.IsNullOrEmpty(Description );
    }

    [XmlAttribute]
    public string Author {get;set;}

    public bool ShouldSerializeAuthor() {
        return !string.IsNullOrEmpty(Author);
    }

    [XmlAttribute]
    public string Publisher {get;set;}

    public bool ShouldSerializePublisher() {
        return !string.IsNullOrEmpty(Publisher);
    }
}

答案 1 :(得分:6)

替代方案:

  • 将您的公共字段切换为属性
  • 使用DefaultValueAttribute属性
  • 定义默认值
  • 使用ContentPropertyAttribute属性
  • 定义内容属性
  • 使用XamlWriter / XamlReader

你最终得到这样的东西:

 [ContentProperty("Books")]
 public class Library {

   private readonly List<Book> m_books = new List<Book>();

   public List<Book> Books { get { return m_books; } }

 }

 public class Book
 {

    [DefaultValue(string.Empty)]
    public string Title { get; set; }

    [DefaultValue(string.Empty)]
    public string Description { get; set; }

    [DefaultValue(string.Empty)]
    public string Author { get; set; }

 }

答案 2 :(得分:1)

public class Books
{
    [XmlElement("Book")]
    public List<Book> BookList;
}

public class Book
{
    [XmlAttribute]
    public string Title;
    [XmlAttribute]
    public string Description;
    [XmlAttribute]
    public string Author;
    [XmlAttribute]
    public string Publisher;
}

class Program
{
    static void Main()
    {
        var books = new Books
        {
            BookList = new List<Book>(new[] 
            {
                new Book 
                {
                    Title = "t1",
                    Description = "d1"
                },
                new Book 
                {
                    Author = "a2",
                    Description = "d2"
                },
                new Book 
                {
                    Author = "a3",
                    Title = "t3",
                    Publisher = "p3"
                },
            })
        };

        var serializer = new XmlSerializer(books.GetType());
        serializer.Serialize(Console.Out, books);
    }
}

如果要从根节点中删除命名空间:

var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(Console.Out, books, namespaces);

另外,我建议您在模型类中使用properties而不是fields来更好地封装:

public class Books
{
    [XmlElement("Book")]
    public List<Book> BookList { get; set; }
}

答案 3 :(得分:1)

另一种选择是使用“指定”属性,例如(类似于“ ShouldSerialize()”的作品):

public bool <<Your XML-property name here>>Specified
{
    get { return <<your condition here (i.e. not null check)>>; }
}