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

5/5/2010 8:58:53 AM #

pingback

Pingback from endyear2012.com

car insurance cheap quote «  End Year 2012

endyear2012.com | Reply

6/21/2010 2:08:52 PM #

tear gas cleanup florida

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

tear gas cleanup florida United States | Reply

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

Network Consulting

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

Network Consulting United States | Reply

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

Neiko

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

Neiko Germany | Reply

11/20/2011 9:34:17 AM #

cheap nokia mobile phones

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

cheap nokia mobile phones United Kingdom | Reply

11/21/2011 11:22:56 AM #

truck insurance quotes

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

truck insurance quotes United States | Reply

11/21/2011 11:48:14 AM #

truck insurance quotes

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.

truck insurance quotes United States | Reply

11/21/2011 12:13:19 PM #

truck insurance quotes

Its a great posting.I really like it.

truck insurance quotes United States | Reply

11/23/2011 6:59:07 AM #

best kettlebell workout

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.

best kettlebell workout United States | Reply

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

Technology Blog

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 !

Technology Blog | Reply

11/25/2011 12:51:17 PM #

Debt Consolidation Loan Calculator

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.

Debt Consolidation Loan Calculator United States | Reply

11/25/2011 2:25:11 PM #

home refinance loan

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.

home refinance loan United States | Reply

11/29/2011 10:40:55 AM #

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

hey $author How to Register a new consultancy firm?

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 United States | Reply

12/21/2011 8:15:47 AM #

Wanna be Fit

, 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!

Wanna be Fit | Reply

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

admin

my own blogroll.

admin | Reply

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

admin

included you guys to my

admin | Reply

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

Rob

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.

Rob | Reply

1/5/2012 12:05:19 PM #

Accu Chek Performa

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

Accu Chek Performa | Reply

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

SEO Guy

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.

SEO Guy | Reply

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

Exam Cram

this is a nice way of extension methods.

Exam Cram United Kingdom | Reply

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

Jennie

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

Jennie | Reply

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

Greg

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

Greg | Reply

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

Jennie

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

Jennie | Reply

1/16/2012 10:30:31 AM #

marketing.eserv2you.com

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

Ella | Reply

1/16/2012 10:30:40 AM #

marketing.eserv2you.com

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

Jennie | Reply

1/27/2012 3:52:42 PM #

canadian newcomer magazine

Thanks  for the share

canadian newcomer magazine | Reply

Add comment


(Will show your Gravatar icon)

  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

Page List