Safe X (ml parsing with XLINQ) [XLinqExtensions helps make XML parsing with .NET's XLINQ a bit safer and easier]
**
This blog has moved to a new location and comments have been disabled.
All old posts, new posts, and comments can be found on The blog of dlaa.me.
See you there!
Comments
Anonymous
May 26, 2011
You do know that XLinq has default support for this? Casting an XAttribute or XElement to the correct type will do the null checks for you. Using the ?? operator to set any default values. Except for nested Element() calls you can just do: string attrValue = (string)someElement.Attribute("Test"); string attrValueAsEmpty = (string)someElement.Attribute("Test") ?? string.Empty; int someNumber = (int?)someElement.Attribute("Number") ?? -1;Anonymous
May 26, 2011
I would rename everything from SafeGet to As to get shorter method names... You approach is nice but I also like the ElasticObject approach : www.amazedsaint.com/.../introducing-elasticobject-for-net-40.htmlAnonymous
May 26, 2011
The comment has been removedAnonymous
May 26, 2011
The comment has been removedAnonymous
May 26, 2011
The comment has been removedAnonymous
June 14, 2011
The comment has been removedAnonymous
June 15, 2011
Jonathan van de Veen, I haven't used SyndicationFeed before, but it looks quite useful - thank you very much for the suggestion! (However, because the documentation doesn't list Windows Phone 7 as a supported platform, my original example that targeted WPF/Silverlight/Phone probably wouldn't have been able to use this class.) PS - Cool blog! :)Anonymous
August 24, 2011
Major anti-pattern! a) XElement is mostly used for construction in my code. b) Perhaps consider using XPath for retrieval of data if you need it to default to "nothing" (string.Empty). c) Your extensions are a classic "Not Invented Here" syndrome solution of a trivial problem (null checks, ?? operator). d) Ever heard about XML Schema and default values in schemas? Also a nice solution requires some investigation instead of instant coding. Consider using standard ways instead of your own extension method library unless you really add something to the framework. Trust me, if you EVER have some else read your code you will be thanked gratefully. Might be less fun, sure.Anonymous
August 25, 2011
HzHz, Thanks for your comments! I tend to blog things I find interesting and maybe even useful in my own projects, but folks are welcome to disagree. :)