14

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?

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
John Lewin
  • 6,050
  • 4
  • 22
  • 20

2 Answers2

17

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
  • 3,004
  • 23
  • 29
  • That returns an instance of XAttribute. No idea how casting it to System.Type is going to be helpful. – Kirk Woll Aug 04 '10 at 23:28
  • Not to System.Type lol, whatever type you need it to be. I'll update my answer. – Necros Aug 04 '10 at 23:30
  • 2
    Nice, didn't know about the type conversion on those classes. Thanks! – Kirk Woll Aug 04 '10 at 23:36
  • 6
    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 Aug 11 '10 at 04:00
  • 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 Lewin Aug 14 '10 at 02:06
  • Note that this construct does NOT work when the attribute is not present (unlike the VB.NET "Extension Property" `.Value` which always returns a `String`, but you can then convert it to other types). (I note Oskar has mentioned this issue.) – Mark Hurd Aug 25 '12 at 09:53
  • SetAttributeValue can write Size and Point types but reading doesn't work: `mainForm.Location = (Point)el.Attribute("Location"); mainForm.Size = (Size)el.Attribute("Size");` – Geograph Oct 03 '18 at 11:48
5

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
  • 76,112
  • 22
  • 180
  • 195
  • 4
    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 Lewin Aug 05 '10 at 00:11
  • @Geograph what do you mean? XML attribute values are strings at heart. If you want it as something else, you must convert it yourself. – Kirk Woll Oct 03 '18 at 14:03