Cleaning up XmlWriter and IXmlSerializable with Extension Methods

by jmorris 4. November 2009 19:46

If you do any work with xml you probably have come across scenarios where you are using an XmlWriter to produce an output stream of xml. Eventually this output stream is either persisted to disk via an XDocument, sent over the wire using a distributed technology such as WCF, Remoting etc., or possibly transformed with XSL/XSLT. A strong example is custom serialization classes that implement IXmlSerializable.  For example:

The class above is a simple data transfer class (DTO) that implements IXmlSerializable so that it can be serialized and/or deserialized from an objet to an xml stream and vice versa. Note: in most cases you would simple mark the class as [Serializable] and/or provide attributes from the System.Xml namespace to provide the same behavior, however in many cases the default implemention will not fit your particular scenario, hence you would implement IXmlSeriable and provide your own custom serialization.

Here is the 'custom' serialization implementation:


While the XmlWriter/XmlReader API's are pretty simple to use, they are also a bit verbose. If you happen to have a fairly large class with many fields, things start to get ugly pretty fast. Typically when I see large classes, I began to think about refactoring into smaller classes when applicable, but that not always the case. Since, most of them time when want serialization/deserialization you simple want to quickly (i.e. less keystrokes) turn the contents and structure of the class into its xml equivalent you are looking at reducing the amount of work needed. This is where extension methods really come in handy:



The result compared to above is a much cleaner, easier to read class:


While extension methods are not new, they do offer unique way of handling situations where you would like to simplify a set of operations without reaching for the traditional static xxxUtil class or creating a customized implementation or wrapper class. In this case,  XmlWriter is a class open for extension via basic inheritance, unlike a sealed class such as System.String, which is the intended purpose of extension methods: extended classes closed to inheritance (sealed).

Tags: , , , , , , ,

Comments (22) -

tear gas cleanup florida
tear gas cleanup florida
6/21/2010 2:08:52 PM #

Your code seems like creeping my mind. But i will try to implement this. Thanks for having it here.. Smile

Reply

Network Consulting
Network Consulting
4/22/2011 2:47:51 PM #

But mostly you would simple mark the class as serializable and provide attributes from the System.Xml namespace to provide the same behavior.

Reply

Neiko
Neiko
4/25/2011 4:00:45 PM #

I like this clean approach, but was hoping to see the deserialize version of it too. Since Framework 3.5 you can also use WriteElementString(xmlTagName, string)

msdn.microsoft.com/de-de/library/aex0e7zs.aspx

Reply

cheap nokia mobile phones
cheap nokia mobile phones
11/20/2011 9:34:17 AM #

Resources like the one particular you mentioned right here will be quite useful to me! I will publish a link to this page on my blog. I’m certain my visitors will discover that extremely useful.UK based best various mobile shopping website

Reply

truck insurance quotes
truck insurance quotes
11/21/2011 11:22:56 AM #

Really interesting articles. I enjoyed reading it. Thanks for sharing a nice info.

Reply

truck insurance quotes
truck insurance quotes
11/21/2011 11:48:14 AM #

I'm very glad to visit this blog. Throughout the starting point, first of all assemble all of the required data that is certainly important pertaining to employing a home purchase loan  correctly.

Reply

truck insurance quotes
truck insurance quotes
11/21/2011 12:13:19 PM #

Its a great posting.I really like it.

Reply

best kettlebell workout
best kettlebell workout
11/23/2011 6:59:07 AM #

I would like to thank you for the amazing information that you put. Indeed helped me understand some basic concepts of this theme. Keep keeping us up to date.

Reply

Technology Blog
Technology Blog
11/23/2011 12:03:26 PM #

Just desire to say your article is as astounding. The clearness in your post is just nice and i could assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work.  Thanks !

Reply

Debt Consolidation Loan Calculator
Debt Consolidation Loan Calculator
11/25/2011 12:51:17 PM #

A quick online visit a debt calculator gives hundreds of links to sites the place where a consumer can plug in a few numbers, and come away with a decent concept of how long and how much it will require to get away from debt.

Reply

home refinance loan
home refinance loan
11/25/2011 2:25:11 PM #

Nationwide Mortgage Loans is the best mortgage lender blog for rates on refinance loan programs, home refinancing, home equity and FHA home loans for 1st time home buyers.

Reply

Jacob|John|Erina|Chewy|Chucky|Bean|Erika|Eileen|Jordan|Ervin|Eliza|Ellie|Eric|Elizabeth|Emmanuel|Bill|Donald|Jesse|Brian|Mark|Bob|Jake|Steve|Harry|Tim|Claire|Phil|John|Toby|Frederick|Ernest|Paul|Wayne|David|John|Fred|Adam|Harry|Louis|Jeremy|Billy|Randy|Aaron|Brandon|Steve|Thomas|Jones|Orlando|Roscoe|Melinda|Sterling|Audra|Stacee|Luciano|Steve|David|Ruben|Bob|Sara|Kim|Mark|Diane|Jesse|Tom|Justin|Jake|Bill|Jon|Brian|Buck|Donald|Chris|Ryan|Adam|Ethan|Michael|Alexander|William|Joshua|Daniel|Jayden|Noah|Anthony|Christopher|Aiden|Matthew|Andrew|Joseph|Logan|James|Benjamin|Elijah|Gabriel|Christian|Nathan|Jackson|Samuel|Tyler|Dylan|Jonathan|Caleb|Nicholas|Gavin|Mason|Evan|Landon|Angel|Lucas|Isaac|Isaiah|Jack|Jose|Kevin|Brayden|Luke|Liam|Carter|Owen|Connor|Zachary|Robert|Hunter|Adrian|Cameron|Wyatt|Chase|Julian|Austin|Charles|Jeremiah|Jason|Juan|Xavier|Luis|Sebastian|Henry|Aidan|Ian|Diego|Nathaniel|Brody|Jesus|Carlos|Tristan|Dominic|Cole|Alex|Cooper|Ayden|Carson|Josiah|Levi|Blake|Eli|Hayden|Bryan|Colton|Parker|Sean|Oliver|Miguel|Kyle|Isabella|Emma|Olivia|Sophia|Ava|Emily|Madison|Abigail|Chloe|Mia|Addison|Alexis|Ella|Samantha|Natalie|Grace|Lily|Alyssa|Ashley|Sarah|Taylor|Hannah|Brianna|Hailey|Kaylee|Lillian|Leah|Anna|Allison|Victoria|Avery|Gabriella|Nevaeh|Kayla|Sofia|Brooklyn|Riley|Evelyn|Savannah|Aubrey|Alexa|Peyton|Makayla|Layla|Lauren|Zoe|Sydney|Audrey|Julia|Jasmine|Arianna|Brooke|Amelia|Morgan|Destiny|Bella|Madelyn|Katherine|Kylie|Maya|Aaliyah|Madeline|Sophie|Kimberly|Kaitlyn|Charlotte|Alexandra|Jocelyn|Maria|Valeria|Andrea|Trinity|Zoey|Gianna|Mackenzie|Jessica|Camila|Faith|Autumn|Ariana|Genesis|Payton|Bailey|Angelina|Caroline|Mariah|Katelyn|Rachel|Vanessa|Molly|Melanie|Serenity|Khloe|Gabrielle|Paige|Mya|Eva|Isabelle
Jacob|John|Erina|Chewy|Chucky|Bean|Erika|Eileen|Jordan|Ervin|Eliza|Ellie|Eric|Elizabeth|Emmanuel|Bill|Donald|Jesse|Brian|Mark|Bob|Jake|Steve|Harry|Tim|Claire|Phil|John|Toby|Frederick|Ernest|Paul|Wayne|David|John|Fred|Adam|Harry|Louis|Jeremy|Billy|Randy|Aaron|Brandon|Steve|Thomas|Jones|Orlando|Roscoe|Melinda|Sterling|Audra|Stacee|Luciano|Steve|David|Ruben|Bob|Sara|Kim|Mark|Diane|Jesse|Tom|Justin|Jake|Bill|Jon|Brian|Buck|Donald|Chris|Ryan|Adam|Ethan|Michael|Alexander|William|Joshua|Daniel|Jayden|Noah|Anthony|Christopher|Aiden|Matthew|Andrew|Joseph|Logan|James|Benjamin|Elijah|Gabriel|Christian|Nathan|Jackson|Samuel|Tyler|Dylan|Jonathan|Caleb|Nicholas|Gavin|Mason|Evan|Landon|Angel|Lucas|Isaac|Isaiah|Jack|Jose|Kevin|Brayden|Luke|Liam|Carter|Owen|Connor|Zachary|Robert|Hunter|Adrian|Cameron|Wyatt|Chase|Julian|Austin|Charles|Jeremiah|Jason|Juan|Xavier|Luis|Sebastian|Henry|Aidan|Ian|Diego|Nathaniel|Brody|Jesus|Carlos|Tristan|Dominic|Cole|Alex|Cooper|Ayden|Carson|Josiah|Levi|Blake|Eli|Hayden|Bryan|Colton|Parker|Sean|Oliver|Miguel|Kyle|Isabella|Emma|Olivia|Sophia|Ava|Emily|Madison|Abigail|Chloe|Mia|Addison|Alexis|Ella|Samantha|Natalie|Grace|Lily|Alyssa|Ashley|Sarah|Taylor|Hannah|Brianna|Hailey|Kaylee|Lillian|Leah|Anna|Allison|Victoria|Avery|Gabriella|Nevaeh|Kayla|Sofia|Brooklyn|Riley|Evelyn|Savannah|Aubrey|Alexa|Peyton|Makayla|Layla|Lauren|Zoe|Sydney|Audrey|Julia|Jasmine|Arianna|Brooke|Amelia|Morgan|Destiny|Bella|Madelyn|Katherine|Kylie|Maya|Aaliyah|Madeline|Sophie|Kimberly|Kaitlyn|Charlotte|Alexandra|Jocelyn|Maria|Valeria|Andrea|Trinity|Zoey|Gianna|Mackenzie|Jessica|Camila|Faith|Autumn|Ariana|Genesis|Payton|Bailey|Angelina|Caroline|Mariah|Katelyn|Rachel|Vanessa|Molly|Melanie|Serenity|Khloe|Gabrielle|Paige|Mya|Eva|Isabelle
11/29/2011 10:40:55 AM #

hey $author How to Register a new consultancy firm?

Reply

Wanna be Fit
Wanna be Fit
12/21/2011 8:15:47 AM #

, sorry if this is a bit off the subject, but... This will make any

person upset:  Diets in reality lead you to gain pounds in the long term and that has turned out to be more and more apparent in the excessive weight epidemic that\\\\\\\'s negatively effecting this unhealthy food, little activity generation.  Are you concerned that stadium seats have

to be increased to help support the rise in

individuals expanding, ahem…girth? It suggests that we are transforming into a even larger place (and never in a good way) of men and women than all of us have ever have prior to now which happens to be before 2 full decades alone.  Our kids are

afflicted with being overweight associated

problems by way of example having diabetes in addition to cardiovascular illnesses.

I read that during a scientific study 70 overweight North american youngsters from the age group of 6 to nineteen have been subjected to many battery of tests to look at

the effect that the diet loaded with fat had on the youthful body.  The final results ended up being eye opening.  All had

high cholesterol levels and also  were within the high-risk group of getting heart disease and coronary heart failure which a few individuals are already

revealing warning signs of.

Could there really be virtually any a cure for anybody?  I believe the

correct answer is yes.  Apparently

just about all we end up needing is definitely plenty of diet and exercise.  We pretty much

all have to embark on performing

it Today!

Reply

admin
admin
12/21/2011 12:36:30 PM #

my own blogroll.

Reply

admin
admin
12/21/2011 12:37:03 PM #

included you guys to my

Reply

Rob
Rob
12/25/2011 2:03:59 AM #

Hey  so many Internet marketing newcomers squander huge amounts of money and time on web pages with zero visitors because they didn’t take the time to learn.

Reply

Accu Chek Performa
Accu Chek Performa
1/5/2012 12:05:19 PM #

Hey  - Great stuff! High Blood Sugar is in my families history so this is the kind of information that really appreciated!

Reply

SEO Guy
SEO Guy
1/12/2012 2:00:21 PM #

What a great blog post. I appreciate your insightful and interesting feedback. I am very surprised that more people do not ponder this topic more often.

Reply

Exam Cram
Exam Cram
1/16/2012 8:46:10 AM #

this is a nice way of extension methods.

Reply

Jennie
Jennie
1/16/2012 10:28:32 AM #

Be quick rich with this  Great program. It has changed my life now and forever
http://richersucess.com

Reply

Greg
Greg
1/16/2012 10:29:04 AM #

Be happier with this  Great software. It has changed my life now and forever
http://richersucess.com

Reply

Jennie
Jennie
1/16/2012 10:29:09 AM #

Be happier with this  Awesome program. It has changed my life now and forever
http://richersucess.com

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Who Am I?

My name is Jeff Morris a software engineer/developer in Southern California, USA. My platform du jour is dotnet, but I dabble on the darkside occasionally.

Tag cloud

Month List

Page List