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 and XAttribute are pretty handy.
This works for SilverLight too (you need to add an assembly reference to System.Core)
FROM JSOP: I already have that rfeerence, and
still isn't available.
FROM VRK: Did you forget adding
to your namespace imports?
FROM JSOP: I got around to playing with the code again, and you're right, I didn't the
in there. I voted your message a 5 because it improved the code, but I didn't mark it as accept alternate
, because technically, you didn't change the base aspect of the tip which was to extend the functionality via an extension method to avoid the inevitable exception, and I actually use the
method without the
method. However, it was a good optimization.