Conditional xml serialization
I have the following C# classes :
public class Books
{
public List<Book> BookList;
}
public class Book
{
public string Title;
public string Description;
public string Author;
public string Publisher;
}
How can I serialize this class into the following XML?
<Books>
<Book Title="t1" Description="d1"/>
<Book Description="d2" Author="a2"/>
<Book Title="t3" Author="a3" Publisher="p3"/>
</Books>
I want the XML to have only those attributes whose values are not null/empty. For example: In the first Book element, author is blank, so it should not be present in the serialized XML.
Solution 1:
You should be able to use the ShouldSerialize*
pattern:
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);
}
}
Solution 2:
Alternative :
- Switch your public fields to properties
- Define default values with the
DefaultValueAttribute
attribute - Define content property with the
ContentPropertyAttribute
attribute - Use XamlWriter/XamlReader
You end up with something like this:
[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; }
}
Solution 3:
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);
}
}
And if you want to remove the namespace from the root node:
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
serializer.Serialize(Console.Out, books, namespaces);
Also I would recommend you using properties instead of fields in your model classes for better encapsulation:
public class Books
{
[XmlElement("Book")]
public List<Book> BookList { get; set; }
}
Solution 4:
Another option is to use "Specified"-property like (works like "ShouldSerialize()"):
public bool <<Your XML-property name here>>Specified
{
get { return <<your condition here (i.e. not null check)>>; }
}