views:

59

answers:

2

Sometimes I'd like to know the reasoning of certain API changes. Since google hasn't helped me with this question, maybe stackoverflow can. Why did Microsoft choose to remove the GetAttribute helper method on xml elements? In the System.Xml world there was XmlElement.GetAttribute("x") like getAttribute in MSXML before it, both of which return either the attribute value or an empty string when missing. With XElement there's SetAttributeValue but GetAttributeValue wasn't implemented.

Certainly it's not too much work to modify logic to test and use the XElement.Attribute("x").Value property but it's not as convenient and providing the utility function one way (SetAttributeValue) but not the other seems weird. Does anyone out there know the reasons behind the decision so that I can rest easily and maybe learn something from it?

+1  A: 

Not sure exactly the reason, but with C# extension methods, you can solve the problem yourself.

public static string GetAttributeValue(this XElement element, XName name)
{
    var attribute = element.Attribute(name);
    return attribute != null ? attribute.Value : null;
}

Allows:

element.GetAttributeValue("myAttributeName");
Kirk Woll
I often end up doing essentially what this function does and certainly this is the best approach IMO but I'm interested in *why* GetAttribute is missing from the API when it's been there in previous models and they provide a helper for the setter
John
+2  A: 

You are supposed to get attribute value like this:

var value = (TYPE) element.Attribute("x");

UPDATE:

Examples:

var value = (string) element.Attribute("x");
var value = (int) element.Attribute("x");

etc.

See this article: http://www.hanselman.com/blog/ImprovingLINQCodeSmellWithExplicitAndImplicitConversionOperators.aspx. Same thing works for attributes.

Necros
That returns an instance of XAttribute. No idea how casting it to System.Type is going to be helpful.
Kirk Woll
Not to System.Type lol, whatever type you need it to be. I'll update my answer.
Necros
Nice, didn't know about the type conversion on those classes. Thanks!
Kirk Woll
I just commented on this topic here (http://mo.notono.us/2010/08/xelement-xattribute-and-explicit.html), but I'm stuck with the thought that the explicit type conversion operators are a bad thing. There is no intellisense for them, and they look like just a garden variety cast. - Kirk's confusion is perfectly understandable.Even though the conversion operators exist, I think I will prefer the extension method, if only because it is explicitly obvious what it does...PS! note that you need to use the nullable type cast if there's a chance the attribute or element does not exist.
Oskar Austegard
Necros, you've exposed something I was unfamiliar with before this posting. Awesome to learn something new, thanks. It's plausible the GetAttribute method was removed to highlight this mechanism but I still wish I had more insight into the decision. I'm not certain I prefer this route but I've been using it bit and when casting to non-string types it removes some extra steps
John