27

Sometimes I want to emulate stored data of my classes without setting up a round trip to the database. For example, let's say I have the following classes:

public class ShoppingCart
{
    public List<CartItem> Items {get; set;}
    public int UserID { get; set; }
}

public class CartItem
{
    public int SkuID { get; set; }
    public int Quantity  { get; set; }
    public double ExtendedCost  { get; set; }
}

Let's say I build a ShoppingCart object in memory and want to "save" it as an XML document. Is this possible via some kind of XDocument.CreateFromPOCO(shoppingCart) method? How about in the other direction: is there a built-in way to create a ShoppingCart object from an XML document like new ShoppingCart(xDoc)?

0

4 Answers 4

61

XmlSerializer is one way to do it. DataContractSerializer is another. Example with XmlSerializer:

using System.Xml;
using System.Xml.Serialization;

//...

ShoppingCart shoppingCart = FetchShoppingCartFromSomewhere();
var serializer = new XmlSerializer(shoppingCart.GetType());
using (var writer = XmlWriter.Create("shoppingcart.xml"))
{
    serializer.Serialize(writer, shoppingCart);
}

and to deserialize it back:

var serializer = new XmlSerializer(typeof(ShoppingCart));
using (var reader = XmlReader.Create("shoppingcart.xml"))
{
    var shoppingCart = (ShoppingCart)serializer.Deserialize(reader);
}

Also for better encapsulation I would recommend you using properties instead of fields in your CartItem class.

3
  • Thanks for the suggestion. I had meant to use properties instead of fields. Fixed. Jul 28, 2010 at 20:17
  • It took me a while to get around to testing this, but it worked perfectly. Thanks! Aug 3, 2010 at 15:54
  • 3
    I wish I could give you an upvote for each time I've navigated to this answer in the past few weeks. It keeps paying dividends! Aug 19, 2010 at 13:24
13

Nicely done. Here is the example to serialize plain POCO to string.

    private string poco2Xml(object obj)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());
        StringBuilder result = new StringBuilder();
        using (var writer = XmlWriter.Create(result))
        {
            serializer.Serialize(writer, obj);
        }
        return result.ToString();
    }
3

You could serialize/deserialize with either the XmlSerializer or the DataContractSerializer.

Annotate your classes with DataContract and DataMember attributes and write something like this to serialize to xml to a file.

ShoppingCart cart = ...
using(FileStream writer = new FileStream(fileName, FileMode.Create))
{
   DataContractSerializer ser = new DataContractSerializer(typeof(ShoppingCart));
   ser.WriteObject(writer, cart);
}
8
  • What if an exception is thrown during the serialization? Jul 28, 2010 at 20:12
  • 1
    @Darin: Which you will often encounter when starting to use the DataContractSerializer. But it usually boils down to attribute annotation of your classes. The XmlSerializer is easier to use as it takes almost whatever you throw at it, but the DataContractSerializer is faster, but requires more "work" to get it up and running. Jul 28, 2010 at 20:26
  • @Mikael, I think you didn't get my point. What I meant was that if an exception is thrown you will leak a file handle which is very bad as it will lock the file and no other process be able to do anything with it until you kill the application. Jul 28, 2010 at 20:28
  • 2
    @Mikael, StackOverflow is a quite well referenced site meaning that what you write here could potentially be seen and irresponsibly used by many people in production code and I think that basic things like disposing disposable resources is a minimum to avoid promoting bad practices. I am sure that there are many people that simply do a copy-paste from blog posts without even realizing the catastrophic implications this might have on their production systems. Jul 28, 2010 at 20:36
  • 1
    @Darin: Very true indeed. I actually copy/pasted my code from MSDN and modified it, so they have work to do as well in their samples. Except the sample is a console app, so any file lock will be dealt with upon the program "crashing". Jul 28, 2010 at 20:56
0

Just mark up what you want to serialize with [XmlElement(name)] (or XmlAttribute, XmlRoot, etc) and then use the XmlSerializer. If you need really custom formating, implement IXmlSerializable.

1
  • 2
    Why not provide a code sample like the other answers on the thread?
    – Konstantin
    Aug 12, 2016 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.