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.
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 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.
02-03-2009 10:00 AM