23

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.

4 Answers 4

47

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I was unclear on how the above solution worked until I read the following post: kjellsj.blogspot.com/2006/02/…
+1 Coolest thing I never knew about :) Just solved a particularly sticky backward-compatibility problem with this gem.
Note that ShouldSerialize method should be public here, unlike other cases like PropertyGrid serialization control where the method can be private and yet not be ignored.
8

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

 }

1 Comment

This should be the answer. ShouldSerialize* works, but [DefaultValue("")] is by far simpler method
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; }
}

4 Comments

Is this actually answering the question about skipping the null/blank items?
Indeed my code works for null elements, but it doesn't work for blank elements (they will still be present in the generated XML).
I believe, when u use properties, by default, the value is null and hence the attributes wont be present in the generated xml unless they r set to empty or any other value.
Fields are also null by default for reference types and the default value for value types.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.