Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

SharePoint Server MVP / Author / MCT / MCTS / MCP / MSc writing about SharePoint and other interesting Microsoft technologies

Even better encapsulation of getting a value from XmlNode

Posted at 2006-10-31 05:08 by Wictor Wilén in .NET , C# , XML with 4 comments.

Marcus writes in his blog how he often ends up with a method that reads an attribute from an XmlNode object. He has optimized his method so that the exception handling will be minimal, since that is pretty expensive.

This is a pretty common scenario for me and I have an even better solution, that does'nt involve exception handling at all.

private static string GetAttribute(XmlNode node, string name)
{
    XmlElement elm = node as XmlElement;
    if (elm == null) {
        return "";
    }
    return elm.GetAttribute(name);
}

This method uses the C# as operator which cast the XmlNode to an XmlElement, if it fails it will return null instead of invoking exception handling.

If you compare the performance of this and Marcus it's about the same using all three of them when there is an attribute with that name on the XmlNode object, but if it is not then this method is faster. 10.000 iterations of these three different implementations with an attribute that does not exist on the Xml node then we get these results:

  • Marcus first method (try/catch): 0,878 seconds
  • Marcus optimized method (foreach): 0,877 seconds
  • My implementation (cast): 0,001 seconds

Comparisons when hitting a correct attribute looks like this for 1.000.000 iterations:

  • Marcus first method (try/catch): 0,200 - 0,240 seconds
  • Marcus optimized method (foreach): 0,133 - 0,284 seconds
  • My implementation (cast): 0,112 - 0,257 seconds

If you have more attributes on the Xml node then Marcus optimized method will start to suffer. These timings are for 1.000.000 iterations and 6 attributes:

  • Marcus first method (try/catch): 0,323 - 0,342 seconds
  • Marcus optimized method (foreach): 0,523 - 0,533 seconds
  • My implementation (cast): 0,309 - 0,310 seconds

You can also write this code inline (and allocate the XmlElement outside the loop so it's only allocated once), i.e. don't create a method for it, and you will have about 10% performance increase on a lot of iterations.

This is what I really like, you may call me an optimization nerd, but that is the beauty of programming...

Anyone have an even better implementation?

Note: The times was measured with QueryPerformanceCounter in Kernel32.dll

Comments and trackbacks

#  Improvement by Craig Nicholson
Screenshot from websnpr I'd change it as follows: private static string GetAttribute(XmlNode node, string name) { XmlElement elm = node as XmlElement; return (elm == null) ? string.Empty : elm.GetAttribute(name); } Firstly use the string.Empty constant as it will make comparisons in the same AppDomain faster and won't create possibly hundreds of useless strings. Secondly there is only a single entry and exit from the method.
#  Improvement by Craig Nicholson
Screenshot from websnpr I'd change it as follows: private static string GetAttribute(XmlNode node, string name) { XmlElement elm = node as XmlElement; return (elm == null) ? string.Empty : elm.GetAttribute(name); } Firstly use the string.Empty constant as it will make comparisons in the same AppDomain faster and won't create possibly hundreds of useless strings. Secondly there is only a single entry and exit from the method.
#  Re: improvement by Wictor
Screenshot from websnpr Thanks, You are correct, using the string.Empty is the right way to do it. I'll post an update to this...
#  Updated: Even better encapsulation of getting a value from XmlNode by Trackback
Screenshot from websnpr I previously suggested a better method for getting attributes for an System.Xml.XmlNode in a response to an post by Marcus. Craig Nicholson highlighted in a comment that the code i provided can be eve...
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.
Real Time Web Analytics