technical banterings of jon davis (a web & software developer)

Here are a couple classes that I threw together for web services and WCF usage where the client is using the same data class library as the server. Microsoft auto-serializes stuff, but when serializing explicitly I prefer to retain an explicit type reference rather passing and manually deserializing a string or byte array. In other words, if I see a web service API return a string or byte[] as an XML or binary serialization of a complex class, I get cranky, because the client should know, without having to use external documentation, how the deserialization is type-mapped.

I'm sure there are a bunch of other uses for these, like saving an object graph to disk in two lines of code, etc. In fact, I'm also using XmlSerialized<T> to convert one compatible object type (full) to another object type (lightweight) and back again.

Being as these were indeed thrown together, I give no guarantees of anything. They worked for me.

Scenario:

// take ..
public string GetMyObject() {
var myObject = new MyType();
var sw = new System.IO.StringWriter();
var serializer = new XmlSerializer(typeof(MyType));
serializer.Serialize(sw, myObject);
return sw.ToString();
}
// .. which is a total pain to deserialize,
// and replace it altogether with ..
public XmlSerialized<MyType> GetMyObject() {
return new XmlSerialized<MyType>(new MyType());
}
// which can be deserialized either manually or "automagically"
// using .Deserialize().

Add comment

About the author

Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.

Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."

Jon was previously employed as a senior .NET developer at a very well-known Internet services company whom you're more likely than not to have directly done business with. However, this blog and all of jondavis.net have no affiliation with, and are not representative of, his former employer in any way.