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 (9)

  1. Steve says:

    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;

  2. Jonx says:

    I would rename everything from SafeGet to As to get shorter method names…

    You approach is nice but I also like the ElasticObject approach : http://www.amazedsaint.com/…/introducing-elasticobject-for-net-40.html

  3. Delay says:

    Steve,

    When looking into this, I'd come across the explicit operators for non-nullable types but that wasn't what I wanted since they'll throw for conversion problems. Your suggestion to use the explicit operators for nullable types is fantastic – I had *not* seen that before and it's quite nice!

    In terms of readability vs. what I show here, I'm thinking they're probably about the same. The approach you show spreads the type information out a bit more (with the cast at the beginning of the line and the fallback at the end), but I like that it's built-in vs. custom. I'll definitely try that out next time I'm working in this area!

    On the topic of nesting Element calls, it's a bummer there doesn't seem to be a built-in provision for that, so I'll probably stick with SafeGetElement for now. That said, there are only two parts to the problem and you've highlighted a great solution for one of them, so thank you very much for sharing! :)

  4. Delay says:

    Jonx,

    That's fair – sometimes I tend toward longer, more explicit names with the hope they'll be more clear to the person reading the code. :)

    I just read the post you link to; ElasticObject looks pretty neat as well! I've subscribed to that blog because some of the other stuff there also interested me.

    Thank you!

  5. Mike Strobel says:

    Steve beat me to the punch :).  Indeed, the ability to use the "??" operator (and, similarly, the "as" operator) with nullable value types is extremely convenient.  Thanks to the C# team for making it possible.  The "??" operator works particularly well with nullable value types because when the right operand is a non-nullable value type, the entire "??" expression resolves to the non-nullable type.

  6. Jonathan van de Veen says:

    Hi David,

    There is really no need to parse RSS yourself!

    Have a look at the SyndicationFeed class and the classes around it. You can just dump in an XmlReader into SyndicationFeed.Load() and you're done.

    Check out this post: jvdveen.blogspot.com/…/pimpin-blog-part-2.html

    Greets,

    Jonathan

  7. Delay says:

    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! :)

  8. HzHz says:

    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.

  9. Delay says:

    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. :)