Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / XML

Using Extension Methods To Avoid XML Problems

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
17 May 2010CPOL 8K   1  
I will just make it a little more compact by omitting the Contains method:public static string GetValue(this XElement root, string name, string defaultValue) { return (string)root.Elements(name).FirstOrDefault() ?? defaultValue; }The explicit operators for XElement...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
10 Jul 2014#realJSOP 3 alternatives  
Extension methods can help you avoid sticky situations.
Please Sign up or sign in to vote.
18 Jul 2014Richard Deeming
This is an alternative for "Using Extension Methods To Avoid XML Problems"
Please Sign up or sign in to vote.
25 Jan 2011Richard Deeming
You can avoid all of this by using the explicit cast operators:public XElement Element{ set { this.MyVar1 = (string)value.Element("MyVar1") ?? "NO VALUE"; this.MyVar2 = (string)value.Element("MyVar2") ?? "NOVAL"; }}Both XElement and XAttribute define...

License

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


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions