views:

163

answers:

2

I implement an xml serialization based on Marc's answer.

Should this be part of the class itself, ie Apple.Serialize/Deserialize?

Although Deserialize would be static in that case, as you might not have an instance to call it on.

Or should I have another class for Serialize/Deserialize? If so, these seem to be generic enough? What should I call it? AppleSerializer seems very specific.

Or something better?

+1  A: 

The Serialize/Deserialize code Marc suggested is not linked to your Apple object in any way.

So I would suggest to declare them static make them a little more generic and put them in some sort of utility class. Like so:

class Util
{
  public static string SerializeToXml<T>(T o) where T: class
  {
    using (var sw = new StringWriter())
    {
      var ser = new XmlSerializer(typeof(T));

      ser.Serialize(sw, o);

      return sw.ToString();
    }
  }

  public static T DeserializeFromXml<T>(string xml) where T: class
  {
    using (var sr = new StringReader(xml))
    {
      var ser = new XmlSerializer(typeof(T))

      return ser.Deserialize(sr) as T;
    }
  }
}
Yannick M.
-1 for no `using` blocks.
John Saunders
I answered the question, I didn't rewrite the serialization code.
Yannick M.
Then let us say, "-1 for propagating a mistake". People copy and paste our code, often without understanding it. We have to be careful when we post code.
John Saunders
Much better: +1.
John Saunders
Hehe, there is always a tradeoff between answering the question, and providing a good solution to his actual problem. Sometimes the person is just not intrested in the best solution. Duct taping as Joel put it I believe.
Yannick M.
XmlSerializer isn't `IDisposable`, and while I'm usually a total pedant (bordering on the psychotic) about `using`, it doesn't matter for `StringReader`/`StringWriter`.
Marc Gravell
I'm considering -1 for 'too many/faulty using blocks'
Henk Holterman
Fixed -- I knew I should have cracked open my IDE. Google tricked me: I stumbled on someone's implementation of XmlReader that did implement IDisposable.
Yannick M.
@Marc: with all due respect, the "contract" with respect to `StringReader` / `StringWriter` does permit Microsoft to change their implementations in such a way that it will make a difference in the future. Also, I see so many problems with missing `using` blocks where it matters right now, that I've decided they should be used everwhere except for WCF proxy classes.
John Saunders
+1  A: 

One flaw of the technique proposed by Yannick M. is that it ties you to a particular implementation of XML Serialization. At the very least, serialization should be a virtual instance operation, so that it may be overridden by derived classes.

Also, do not start off using the XML Serializer. Use the Data Contract serializer instead. It is more efficient and faster, at the cost of not allowing you to specify the exact structure of the generated XML (which you may not need to do at all).

John Saunders
Marc covered this already in his response to the initial question. (Feel free to downvote him aswell;-)
Yannick M.
I agree that in theory not being tied to an implementation is a good thing, however, wouldn't another implementation of serialization require new Attributes on the object you are trying to serialize? Thus your code will always be a bit implementation dependant?
Yannick M.
I meant that a `Util.SerializeToXml` method that always uses the XML Serializer ties you to that technology. I don't like hiding an implementation dependency in the middle of a utility class, where nobody will be looking for it.
John Saunders
I hate to say it, but `DataContractSerializer` **isn't** designed to be an xml serializer in the same way that `XmlSerializer` is... if the requirement is to use xml (which **commonly**, and **increasingly** uses attributes etc), I'd use either `XmlSerializer` or do it with LINQ-to-XML.
Marc Gravell
@Marc: I was assuming that the format doesn't matter, since he doesn't say that it does matter.
John Saunders
Thanks guys. Yeah I don't really care about the format because I just need to be able to serialize/deserialize and have the output/input as string.
Joan Venge