Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / XML
Tip/Trick

.Net - Use The Framework

Rate me:
Please Sign up or sign in to vote.
4.82/5 (22 votes)
22 May 2010CPOL 38.3K   5   21
Don't reinvent the wheel - use the one that's already on the cart.
I've found that there are many parts of the .Net framework that can be used "on the sly". For instance, there was a "Quick Answer" question posted today that presented this problem:

Given a string of configuration settings, like this (with more items than I'm showing here):

string text = "screen_fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\"


The user asked how to parse the string so he could change the settings in the string. Most of you may have already recognized this string for what it probably is - something from a XML file. There were a couple of responses to this question that suggested using the string.Split() method, or even Regex (wholly inappropriate IMHO), but there is an easier way, and the framework comes to the rescue.

Simply make the string into an acceptable XML format by added a name and the necessary brackets so that it takes on the look of an actual XML element, and then it's a simple matter to process the element instead of its string counterpart, like so:

// our original string
string text = "screen_fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\"";
// add our XML endcaps
text = string.Format("<TEST {0} />", text);
//Parse the string into an XML element
XElement element = XElement.Parse(text);
// change the value(s) we want to change
element.Attribute("screen_fadetimeout").Value = "5";
// and return our string to its original configuration
text = element.ToString().Replace("<TEST","").Replace("/>","").Trim();


My tip is to use the framework where possible instead of reinventing the wheel or doing things the hard way.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tarek Elqusi31-Jan-13 13:09
professionalTarek Elqusi31-Jan-13 13:09 
QuestionMy 4: In general I agree... Pin
Andreas Gieriet3-Jun-12 21:44
professionalAndreas Gieriet3-Jun-12 21:44 
GeneralWhy you think Rexex is inappropiate? - I'm no fan of it - ju... Pin
johannesnestler24-Feb-12 0:54
johannesnestler24-Feb-12 0:54 
GeneralReason for my vote of 5 nice one Pin
Nikhil_S23-Feb-12 18:11
professionalNikhil_S23-Feb-12 18:11 
GeneralReason for my vote of 5 considerable advice Pin
Pranit Kothari8-Jan-12 4:23
Pranit Kothari8-Jan-12 4:23 
GeneralReason for my vote of 5 My 5! Pretty Logical. This also gave... Pin
RaisKazi1-Nov-11 20:51
RaisKazi1-Nov-11 20:51 
GeneralReason for my vote of 5 Learning the Framework is more effic... Pin
Eddy Vluggen16-Sep-11 6:58
professionalEddy Vluggen16-Sep-11 6:58 
GeneralI agree Pin
PIEBALDconsult27-Jan-12 8:28
mvePIEBALDconsult27-Jan-12 8:28 
GeneralSo right! Pin
johannesnestler25-May-10 5:01
johannesnestler25-May-10 5:01 
GeneralRe: So right! Pin
#realJSOP26-May-10 11:27
mve#realJSOP26-May-10 11:27 
GeneralMy vote of 2 Pin
TheGreatAndPowerfulOz6-Apr-10 12:45
TheGreatAndPowerfulOz6-Apr-10 12:45 
GeneralRe: My vote of 2 Pin
#realJSOP6-Apr-10 23:34
mve#realJSOP6-Apr-10 23:34 
Generala better way Pin
TheGreatAndPowerfulOz6-Apr-10 12:44
TheGreatAndPowerfulOz6-Apr-10 12:44 
GeneralRe: a better way Pin
#realJSOP6-Apr-10 23:32
mve#realJSOP6-Apr-10 23:32 
GeneralRe: a better way Pin
TheGreatAndPowerfulOz7-Apr-10 6:06
TheGreatAndPowerfulOz7-Apr-10 6:06 
GeneralRe: a better way Pin
#realJSOP7-Apr-10 23:27
mve#realJSOP7-Apr-10 23:27 
GeneralRe: a better way Pin
TheGreatAndPowerfulOz22-Apr-10 4:09
TheGreatAndPowerfulOz22-Apr-10 4:09 
GeneralGood tip but... Pin
kornman0013-Jan-10 3:27
kornman0013-Jan-10 3:27 
GeneralRe: Good tip but... Pin
#realJSOP13-Jan-10 3:45
mve#realJSOP13-Jan-10 3:45 
GeneralRe: Good tip but... Pin
kornman0013-Jan-10 5:13
kornman0013-Jan-10 5:13 
I realize that was the point of your tip, but I was trying to say it should come with the note that you're using a framework device for something it wasn't explicitly designed for and thus may come with some over head. Don't just assume that experienced programmers will be reading your tips and will have that (usually) inherited knowledge.

The only thing "far better" that your tip demonstrates is ease of use. Showing profiling results against Split/Regex + String.Format could prove or disprove that it is also "far better" in terms of perfomance.

Was just my 2c
GeneralRe: Good tip but... Pin
Luc Pattyn17-Jan-10 3:45
sitebuilderLuc Pattyn17-Jan-10 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.