Click here to Skip to main content
15,392,679 members
Articles / Programming Languages / XML
Alternative
Tip/Trick
Posted 25 Jan 2011

Tagged as

Stats

8K views
1 bookmarked

Using Extension Methods To Avoid XML Problems

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
25 Jan 2011CPOL
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...
You can avoid all of this by using the explicit cast operators:

C#
public XElement Element
{
    set
    {
        this.MyVar1 = (string)value.Element("MyVar1") ?? "NO VALUE";
        this.MyVar2 = (string)value.Element("MyVar2") ?? "NOVAL";
    }
}


Both XElement and XAttribute define explicit cast operators for:

string,<br />
bool, bool?, <br />
int, int?, <br />
long, long?, <br />
uint, uint?, <br />
ulong, ulong?, <br />
float, float?, <br />
double, double?, <br />
decimal, decimal?, <br />
DateTime, DateTime?, <br />
DateTimeOffset, DateTimeOffset?, <br />
TimeSpan, TimeSpan?, <br />
Guid, Guid?


For string and nullable value types, the operators simply return null if the element or attribute is null; for non-nullable value types, they throw an ArgumentNullException for null elements or attributes.

License

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

Share

About the Author

Richard Deeming
Software Developer CodeProject
United Kingdom United Kingdom
I started writing code when I was 8, with my trusty ZX Spectrum and a subscription to "Input" magazine. Spent many a happy hour in the school's computer labs with the BBC Micros and our two DOS PCs.

After a brief detour into the world of Maths, I found my way back into programming during my degree via free copies of Delphi and Visual C++ given away with computing magazines.

I went straight from my degree into my first programming job, at Trinet Ltd. Eleven years later, the company merged to become ArcomIT. Three years after that, our project manager left to set up Nevalee Business Solutions, and took me with him. Since then, we've taken on four more members of staff, and more work than you can shake a stick at. Smile | :)

Between writing custom code to integrate with Visma Business, developing web portals to streamline operations for a large multi-national customer, and maintaining RedAtlas, our general aviation airport management system, there's certainly never a dull day in the office!

Outside of work, I enjoy real ale and decent books, and when I get the chance I "tinkle the ivories" on my Technics organ.

Comments and Discussions

 
GeneralBut the code is much simpler with the explicit cast operator... Pin
Richard Deeming25-Jan-11 10:05
mveRichard Deeming25-Jan-11 10:05 
GeneralUsing extension methods, I a) don't have to worry about null... Pin
#realJSOP25-Jan-11 10:02
mva#realJSOP25-Jan-11 10:02 
GeneralRe: But the code is much simpler with the explicit cast operator... Pin
Richard Deeming25-Jan-11 10:08
mveRichard Deeming25-Jan-11 10:08 

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.