If you want to convert a custom object [Product] to a string by using the XmlSerializer class, you can use the StringWriter class like:

           StringBuilder result = new StringBuilder(string.Empty);

           // Get the product from filesystem
           Product product = Load();

           // Convert object to string by using XmlSerializer
           using (StringWriter writer = new StringWriter(result))
               XmlSerializer xs = new XmlSerializer(typeof(Product));
               xs.Serialize(writer, product);

2 Replies to “Convert object to string by using XmlSerializer”

  1. Very elegant solution compared to the other solutions I was finding online. Thanks for posting this. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts