Last active
August 29, 2015 14:02
-
-
Save Injac/eba3b8a5fd60938bc87d to your computer and use it in GitHub Desktop.
Useful XDocument extension to check if an element exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class XDocExtensions | |
| { | |
| public static bool CheckIfElementExists(this XDocument doc, string elementName) | |
| { | |
| return doc.Descendants().FirstOrDefault(e => e.Name.LocalName.Equals(elementName)) != null; | |
| } | |
| } | |
| //HOW TO USE IT | |
| var test = yourdoc.CheckIfElementExists("yourelement") | |
| ? yourdoc.Descendants().FirstOrDefault(d => d.Name.LocalName.Equals("yourelement")) | |
| .Value | |
| : null; | |
| //CODESNIPPET BROUGHT TO YOU BY AWSOMEDEVSIGNER | |
| /* | |
| .@+` | |
| +@+` | |
| ;@@@` | |
| :@@@@. | |
| ,#@@@@; ` | |
| .#@@#@@+` ; | |
| `#@@#++@@, `+. | |
| `+@@#+++@@' :@, | |
| `+@@#++++@@#`'@; | |
| '@@#+++++#@@'@@+` | |
| '@@#++++++#@@@@@@` | |
| `'@@#+'++++++@@@@@@: | |
| '@@@''+'+++++@@@@@@# | |
| +@@@'+''+++++'#@@@@#@, | |
| `'@@#+'++++++++++@@@##@' | |
| `` `+@@#+'+++++++++++@@@#+@@. | |
| .+. .#@@#+'+'++++++++++#@@#'@@' | |
| .## ,#@@#''+'++++++++++++@@#+#@@. | |
| ,#@' :#@@#''''+'+++++++++++@@#++#@' `` | |
| ,#@@. `;@@@#+''''''+++++++++++#@#'+#@#. ,: | |
| ;@@#@ `'@@@#'+''''''++++++++++++@#+++@@+` ;' | |
| `'@@@@+.+@@@#''''''''''+'+++++++++@#+++#@@: +#` | |
| `+@@@@@#@@@@+'''''''''''+++++++++++##'+++@@#` +@, | |
| .#@@#;@@@@@@+''''''''''''++++++++++'+#++++#@@+.#@+ | |
| ,#@@+''@@@@#+''''''''''''''+++++++++++#+++++@@@+#@@, | |
| `;@@@+'''@@@#''''''''''''''''+++++++++++++++++#@@@@@@; | |
| `'@@@+''''@@#'''''''''''''''''++++++'+++++++++++#@@@@@#. | |
| .#@@#''''''@#''''''''''''''''''+'+'+++++++++++++++@@@@@@; | |
| :#@@+'''''''+'''''''''''''''''''''+'++++++++++++++++@@@+@#` | |
| `'@@@+''''''''''''''''''''''''''''''+++'++++++++++++++#@@+@@: | |
| .+@@#''''''''''''+''''''''''''''''''''++'+++++++++++++++#@+#@#` | |
| `;#@@#''''''''''''''''''''''''''''''''+'+'+++++++++++++++++#++@@; | |
| .+@@@+'''''''''''''''''''''''''''''''''+'+'++++++++++++++++++++#@#. | |
| :#@@#+''''''''''''''''''''''''''''''''''''+''++++++++++++++++++++@@; | |
| `'@@@#'''+'''''''''''''''''''''''''''''''''++++++++++++++++++++++++#@#. | |
| `:#@@#+''+'''''''''''''''''#+'+''''''''''''''#''+'+++#++++++++++++++++@@+` | |
| .'@@@#''''''''''''''''''''''#+''''''''''''+''+#++'+'+##+++++++++++++++++@@; | |
| :@@@#+'''+'''''''''''''''''''##'''''''''''+''+++'+'+++#++++++++++++++++++#@@. | |
| ,+@@@#'''''''''''''''''''''''''##'''''''''''++'+#+++'++#++++++++++++++++++++@@+` | |
| .'#@@@+''''''''''''''''''''''''''+#''''''''''''''++'++++#+++++++++++++++++++++#@@' | |
| `;#@@@#''+'''''''''''''''''''''''''+#''''''''+'''+'#+++++##'+++++++++++++++++++++#@@: | |
| `:+@@@#+'''''''''''''''''''''''''''''+#'''''''''''+'+#+'+++@++++++++++++++++++++++++@@#, | |
| `,+@@@@#+''+'''''''''''''''''''''''''''+#'''''''''''''#+'+'+##++++++++++++++++++++++++#@@#. | |
| .+@@@@#+''+''+''''''''''''''''''''''''''+#+'''''''+'''+#+'++##++++++++++++++++++++++++++#@@#. | |
| ,+#@@@#++++++'''''''''''''''''''''''''''''+@+'''''''++''##'++#@#+++++++++++++++++++++++++++#@@#. | |
| :#@@@#++++++'++'+''''''+''''''''''''''''''+@#'''''''#+++@+'++@@+++++++++++++++++++++++++++++#@@#. | |
| .;@@@++'+++'+++''''''+'''''''''''''''''''+@#++'+''+#'++@'++#@#+++++++++++++++##@#+++++++++++#@@#, | |
| .+@@@#+++++''++++''''+##+++''''''''''''+@#+''''+@+''@#''+@@+++++++++++++##@@@@#++++++++++++#@@@: | |
| '@@@@+'++++++++++'+@@@@@@@###+++'''''+@@+''+'#@+'+@+++#@#'++++++++##@@@@@@@@#+++++++++++++#@@@;` | |
| `'#@@@+''+++++++++++#@@##@#@@@@@@@@#++'+@@#++'+#@''#@+'+@@++++++##@@@@@@##':#@@++++++++++##@@@@@#;` | |
| .+@@@#+'++++++++++''+@@+,,,::;'+#@@@@@@@#@@#'''#@#'+@#++@@@####@@@@@@#+':,...'@@++++++++++#@@@+;,` | |
| `,#@@+++++++++++++'+@@@,.`......,,;+#@@@@@@@#@#@@#+#@@##@@@@@@@##+':,,,....,.;@@+++++++++++#@@'` | |
| ,@@@++'++++++++'+#@@'............`,:;+#@@@@@@@@@@@@@@@###+';;,......,..,,.,:@@#+++++++++++##@+. | |
| +@@#'+++++++++++@@#....................,:;;;;;'';';:,..............,....,.,@@+++++++++++++#@@#. | |
| ,@@@+'+++++++'++@@:......................................................,,+@#++++++++++++#@@'` | |
| '@@+'+++++++++#@#.......................................................,.'@#++++++++++++@@@. | |
| ,@@#'++++'++'+@@;.........................................................;@#+++++++++++#@@' | |
| `@@@#####+++++@@,.........................................................:@#++++#######@@#. | |
| +@@++++###@@@@#.........................................................,,@@#@#####+++#@@+ | |
| ```;@@##++++++++@+.........................................................,,@@#+++++++++#@@:` | |
| ,'+##@@@##@@@@###@;........................................................,..#@#++++++###@@@#+'. | |
| ;@@@@@@@@+++++##@@@:........................................................,..'@#+##@@@##@@@@@@@#: | |
| :@@@@@@@@@++++++++#@,...........................................................;@@####++++#@@@@@@@@, | |
| .@@@@#;:+@#++++++++@@.`,+#@@@@@@@@@@#@@@@@#@#+;;;;;;;':.:+#@@@@#@@@@@@@@@@@@@@',.,@@++++++++#@@;;#@@@#` | |
| `'@@@@...:#@#+++++++@#..+@@@@@@@@@@@@@@@@@@@@@@@';;;;;;,:#@@@@@@@@@@@@@@@@@@@@@@+.,#@#+++++++@@@,.,@@@@; | |
| `@@@@;....;#@#+++++#@#.:@@@#@@@@@@@@@@@@@@@@@@@@':::::::+@@@#@@@@@@@@@@@@@@@@@@@#:,#@#+++++++@@#...'@@@+` | |
| .@@@#......;@@#####@@+.;@@++###############@@@@@@@@@@@@@@@@++###############@@@@@',+@####+++#@#:..`,#@@#` | |
| ,@@@+`......;@@#+++#@'.;@@+#,``````````````:@@@@@;;;;;;;@@@+#:`````````````.:@@@@',+@@##+++#@#:.....#@@#` | |
| .@@@'`...`...'@@+++#@;`;@@+#````` `` `````.#@@@@``.....#@@+#.```````.....,,:@@@@#+#@@+++##@#,......+@@#` | |
| `@@@'..;+....:#@@++#@###@@+#:,,.......,,::::@@@@@#######@@@+#:,,,,.,,,,:::;;'@@@@@@@@@+++#@#,...+:..+@@#` | |
| `#@@'..,#:`.++,#@#+#@@@@@@##:::,,,,,,::::;;'@@@@@@@@@@@@@@@+#;:,,,..,,,,,:;;'@@@@@@@@@++#@@#;..;#,..+@@' | |
| `'@@+`..'#,;#:.:@@##@#@@@@##:,,,,....,,.`.`,#@@@@@@@@@@@@@@##. `..`````.:.`.@@@@#;;#@##@#::#:,#:...#@@' | |
| ;@@#....+##;..,#@@@#,,+@@#+` `.,,.,,. `:#@@@@#####@@@@@##:. `.,,..,,` `:@@@@',:@@@@@;..'##+...,#@@: | |
| ,@@@,...:@'...,#@@@#..;@@#@:` ````` `:;'@@@@#+'''+#@@@@##;:,. ```` .:;'@@@@',:#@@@@....+@,...;@@@. | |
| .@@@;...+##...,@@@@#..'@@##;;,.``` ``,:;;:,#@@@#++++##@@@@##.,:::,,,,,,:;;::@@@@'.,#@@@@...,##'...'@@#` | |
| `#@@',.;#:+',.:@@@@+..'@@##::;;::::::;;:.`.#@@@@@@@@@@@@@@@#.`..,::::::::..,@@@@',,#@@@@...+';#:..+@@+ | |
| +@@#.,#;.:#:.:@@@#'..;@@@#..,,:::::,,.```.#@@@@@@@@@@@@@@@#.``````....``..,@@@@',.+@@@@..;#:.'+..+@@; | |
| :@@@,.,...,..:@@@;:..;@@@#```````````````.#@@@#+'';;'+#@@@#.```````````..`,@@@@',.'#@@@..,,..,,.,#@@. | |
| `@@@:..,...,,:#@@,...;@@@#:::::::::::::::'@@@@:......`;@@@#;::::::::::::::'@@@@'.,,'@@@.,.,.....,@@# | |
| `#@@;.,.,..,,:#@#,...;@@@@@@@@@@@@@@@@@@@@@@@@;.......+@@@@@@@@@@@@@@@@@@@@@@@@',,,'@@@,,,.,,,,,'@@+ | |
| +@@#,.,,,,,.:@@@:...:@@@@@@@@@@@@@@@@@@@@@@@@;.......'@@@@@@@@@@@@@@@@@@@@@@@#:,,,'@@@,.,,,,..,#@@; | |
| +@@@',,,,,,,:@@@:....'#@@@@@@@@@@@@@@@@@@@@#+........,+@@@@@@@@@@@@@@@@@@@@@@;,,,,'@@@,,,,,,,,'@@@' | |
| +@@@@#';;;::;@@@:.....:;''''''''''''''''''':..........,;;''''''''''''''''''':,,,,:'###:::;;;'+@@@@' | |
| `#@@@@@@@@@@@@@@@;,.................................................,,,,..,,,,,,,::;++###@@@@@@@@@@+ | |
| `@@@@@@@@@@@@@@@@;::,,.................................,............,...,,,,,,,::::;''+##@@@@@@@@@@# | |
| .@@@@@@@@@@@@@@@@;:::,,.............................................,,,,.,,,,:::::;;'''+#@@@@@@@#@@@ | |
| ,@@@+#####@##@@@@;::::::,...........................................,...,,,:::::;;;;'''++#######+#@@` | |
| :@@#++++++++#@@@@;:::::::::,......................................,,..,,::;:::'+;'';;''+'++++++++#@@. | |
| '@@#++++++++#@@@@;::::::::::::,,,...............................,,,,,:::::::;++:;'';';'+'+++++++++@@, | |
| +@@#+++++++++@@@@;::::::::::::::::,,,.....................,,.,,,,:::::::::;+#;..,;;;;''+'+++++++++@@: | |
| #@@++++++++++@@@@;::::::::::::::::::::::::::::::::::::::::::::::::;:::::;+@+, ``:':;'''+'+++++++++@@' | |
| @@@++++++++++@@@@::::::::::::::::::::::::::::::::::::::::::::::::::::::'##;` .;+':;''++++++++++++@@' | |
| @@#++++++++++@@@@':::::::::::::::::::::::::::::::::::::::::::::::::::;+#',` `,++;:;'+++#++++++++++@@+ | |
| `#@#++++++++++#@@@#::::::::::::::::::::::::::::::::::::::::::::::::::+@#:````'@'::;'+###+++++++++++#@# | |
| `@@#+++++++++++#@@@+;:::::::::;''';;::::::::::::::::::::::::::::::;'@@@+.``:##;;;:+###@#+++++++++++#@# | |
| `@@#++++++++++++#@@@#;::::::::;''++###++++'''';;;;;;;;;;;;;;;;;;;'#@#+;++:'#':::;+@@@@#++++++++++++#@# | |
| .@@#+++++++++++++#@@@#'::::::::::::::;''++#+###@@@@#@###@@##@@@@@@@@';;;#@#;:::;+@@@@#+++++++++++++#@@` | |
| .@@+++++++++++++++#@@@@':::::::::::::::::::::::::::;;''++####@@@@@@@@@#@@@+;;;;#@@@@#++++++++++++++#@@` | |
| .@@++++++++++++++++#@@@@';:::::::::::::::::::::::::::::::::::;;;;;'''+++++';;;#@@@@+++++++++++++++++@@` | |
| .@@+++++++++++++++++#@@@@+::::::::::::::::::::::::::::::::::::::::::::::;;::'#@@@@++++'+++++++++++++@@` | |
| .@@++++++++++++++++'++@@@@#;:::::::::::::::::::::::::::::::::::::::::::;:::+@@@@@#+++''+++++++++++++@@` | |
| .@@++++++++++++++'++'+@@@@@#':::::::::::::::::::::::::::::::::::;;;;:;;;:;+@@@@@@#+++''+++++++++++'+@@` | |
| .@@++++++++++++++++'++@@#@@@@'::::::::::::::::::::::::::::::::::::::::;:;+@@@@+@@+++'''+++++++++++++@@. | |
| ,@@++++++++++++++++++'@#.+@@@@'::::::::::::::::::::::::::::::::::;;:;::;#@@@@;.#@++++'+++''+++++'+++@@. | |
| ,@@++++'+++++''+#+++''@#.`'@@@@+:::::;:::::::::::::::::::::::::::::;::'#@@@@, ,#@'++''+++''+'+++++++@@, | |
| ,@@++++'''+''''+#+'+''@#. `:#@@@#;::::;:;:::::::::::::::::::::;;::;:;'@@@@+, ,@@+++''#'+''+'+'+'+++@@. | |
| .@@++++''''''''+#+++''@#. ,#@@@#;:::;;;;:::::::::::::::::::;';:::;+@@@@'` ,@@+++''+'+'''''''''++@#` | |
| .@@+''+++'+''''+#'+'+;@@. .#@@@@;:::;'::::::::::::::::::;;;'::;;+@@@@' :@@'+'+'#++''''''+'+'+@@` | |
| .@@+''+++''''''+#'+'''@@, `'@@@#+:;;+::::::::::::::::::::'+::;#@@@@: :@@'''+'#''''''''+'+'+@@` | |
| .@@+''++''+''''+#'''''#@: ;@@@#+:;#;:::::::::::::::;;::#+:'#@@@#. ;@#+''''#''''''''#'+'+@@` | |
| .@@+''+#'''''''+@+''''#@: :#@@@#;++;;:::::::::::::;:;;@''#@@@+` '@#'''''@''''''''#'''+@@` | |
| .@@+''+#'''''''+@+''''#@; .+@@@###;::::::::;:;;:::;:'@##@@@;` '@#''''+@'''''''+#'''+@@` | |
| `@@+''+@'''''''+@#''''#@; `'@#@@@':::;;;;;:::;:;:::+@@@@#, +@#''''#@'''''''+@'''#@# | |
| @@+''+@+''''''+@@''''+@'` :@@@@+:;::::::::::::;:;#@@@+. +@++'''@#'''''''+@+''#@# | |
| #@#''+@+'''''''@@+'''+@+` ,#@@#;:::::::;;:;;::;+@@@' #@+'''+@#'''''''#@+''#@' | |
| +@#''+@#'''''''@@#'''+@#` :@@@@++++'''''''+++#@@@@, @@+'''#@#'''''''#@+''#@: | |
| :@@''+@#+''''''@@@''''@@` `+@@@@@@@@@@@@@@@@@@@@@#` .@#+'''@@#+'''''+@@+''#@. | |
| .@@''#@@+''''''#@@+'+'#@. .#@@@@@@@@@@@@@@@@@@@#, ,@#'+'+@@#''''''#@@+'+@@` | |
| `#@+'#@@#''''''#@@@+''#@, `;'+##@@@@@@@@@@##+:` '@#'+'#@@#''''''#@@+'+@# | |
| +@+'#@@#+'''''#@@@#''#@' ``````.,,,,,,``` #@+''#@#@#'''''+@@@+'#@' | |
| '@#'#@@@+'''''#@'+@+'+@+ `@@+'+@+'@+'''''#@@@#'#@: | |
| ,@#'#@#@@'''''#@',##+'@#` .@@++@#.+@+''''+@@#@+'@#. | |
| `#@++@;+@#''''+@+`:@@+##, :@#+##,`@@+''''#@''@+'@+ | |
| ;@+#@:.#@+'''+@#` ;@@@@: ;@@@@: `@@+'''+@#`'@+#@: | |
| `@@#@: ,@#+''+@@` `'@@@' `+@@@; ,@@'''+@@. '@#@#` | |
| '@#@; ;@#'''@@, `;@@# .@@#; :@#+'+@#, `+@#@; | |
| .#@@'` ;@#+'@@: ;#@. :@@: '@#'+@#, `#@@#` | |
| ;@@+` ;@@#@@' :#' +#, +@@#@#, `@@@, | |
| +@#` ;@@@@# .'` `;` `#@@@#, .@@; | |
| `#@. ,#@@#` ` ,#@@#. :@'` | |
| .#: ,#@@, :@@'` '+` | |
| .: `'@; '@; ;` | |
| :; `', | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment