Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Don't Overcomplicate The Issue

image I felt compelled to write after seeing this very same issue come up twice in the last week or so, once for a fellow developer and today for me.

The Problem

We use a DataSet for assistance with testing our data layer (think NDBUnit).  The problem we have is that when you generate the DataSet the XSD created contains all these table adapters that bloat the file, in our app the file bloats to about five times the regular size.  We've found that deleting the table adapters on the file makes the file open up so much faster.  However, every time we make a change to the database schema we have to regenerate the DataSet so that it matches the schema.  As the table count has grown deleting these table adapters by hand has grown wearisome.  All this is merely set up for me over-fixing the problem.

My First (Wrong) Thought

I thought to myself, "hey this is XML I'll just load up and XML document and query out the nodes (TableAdapters) I don't need and remove them from the document and save it back."  While writing a quick bit of code I found writing more code than I needed (at this point I was only at 6 lines).  Despite the file being XML, I don't need to treat it as XML.  Understand that nothing about getting rid of the TableAdapters relies on the "XML-ness" of the file, it's all just text. Reminding myself of that...here's what I came up with...

The Second (Correct) Solution

Simply treat the file as text and remove the piece you need, the only caveat is that I'm dealing with XML and so if I remove a "piece" I have to remove the whole "piece" (where "piece" means XmlNode).  The solution below simply matches TableAdapter nodes and replaces them with empty text.  Simple.

   1: var path = @"C:\dev\sandbox\Website\src\IntegrationTests\TestData\Database.xsd";
   2: var text = File.ReadAllText(path);
   3: File.WriteAllText(path, Regex.Replace(text, "<TableAdapter.*?>.*?</TableAdapter>", "", RegexOptions.Singleline));

The Point

The point is the first solution, while correct, was more complex than it needed to be.  Using a little bit of RegEx to wipe clean certain nodes, I get exactly what I want in three lines of code.  Moral of the story?  Don't overcomplicate what doesn't need to be.


Posted 02-03-2009 10:00 AM by Tim Barcz

[Advertisement]

Comments

Arjan`s World » LINKBLOG for February 3, 2009 wrote Arjan`s World &raquo; LINKBLOG for February 3, 2009
on 02-03-2009 4:18 PM

Pingback from  Arjan`s World    &raquo; LINKBLOG for February 3, 2009

Christopher Bennage wrote re: Don't Overcomplicate The Issue
on 02-04-2009 11:15 AM

Excellent, excellent point. I often fall into this trap, and I have to stop myself and ask if I'm actually solving the right problem.

Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew
on 02-04-2009 11:58 AM

Pingback from  Dew Drop - February 4, 2009 | Alvin Ashcraft's Morning Dew

Jon von Gillern wrote re: Don't Overcomplicate The Issue
on 02-04-2009 1:29 PM

If you find your regex.replace call is taking longer than you'd like, I'd recommend the pattern from:

"<TableAdapter.*?>.*?</TableAdapter>"

to

"<TableAdapter[^>]*>.*?</TableAdapter>"

The long and short of it is that ".*?" the regex engine has to do a whole ton of backtracking, by using the negated character class "[^>", you eliminate the need for backtracking. Unfortunately, in this case it will only work for the first instance of ".*?" and not the second.

Anonymous wrote re: Don't Overcomplicate The Issue
on 02-14-2009 12:23 AM

DataSets are trash

devlicio.us wrote re: Don't Overcomplicate The Issue
on 05-08-2011 7:45 PM

Don t overcomplicate the issue.. WTF? :)

devlicio.us wrote re: Don't Overcomplicate The Issue
on 06-22-2011 12:01 PM

Don t overcomplicate the issue.. Retweeted it :)

http://bestmedicineonline.info wrote re: Don't Overcomplicate The Issue
on 02-15-2013 3:31 PM

D0MwMj Wow, great article post. Awesome.

http://bestmedicineonline.info wrote re: Don't Overcomplicate The Issue
on 02-15-2013 3:31 PM

Qxu8xU Im thankful for the article post.Thanks Again. Fantastic.

buy social bookmarks wrote re: Don't Overcomplicate The Issue
on 04-20-2013 8:59 AM

HQGgo0 I really liked your blog article.Really thank you! Cool.

nice seo guys wrote re: Don't Overcomplicate The Issue
on 11-20-2013 9:05 AM

MqfHpC A big thank you for your article post.Really thank you! Keep writing.

check it out wrote re: Don't Overcomplicate The Issue
on 01-21-2014 10:13 PM

T6BXOC Awesome blog article. Want more.

awesome things! wrote re: Don't Overcomplicate The Issue
on 02-05-2014 1:57 PM

muaea0 I cannot thank you enough for the article.

best money here wrote re: Don't Overcomplicate The Issue
on 03-25-2014 3:20 PM

W43GdK I appreciate you sharing this blog.Really looking forward to read more. Fantastic.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)