<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>#js#js | Jeff Sharp</title>
	<atom:link href="http://sharpjs.net/feed" rel="self" type="application/rss+xml" />
	<link>http://sharpjs.net</link>
	<description>Jeff Sharp</description>
	<lastBuildDate>Thu, 17 May 2012 02:32:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>XPathNavigator.MoveToRoot() Gotcha</title>
		<link>http://sharpjs.net/2012/05/16/xpathnavigator-movetoroot-gotcha</link>
		<comments>http://sharpjs.net/2012/05/16/xpathnavigator-movetoroot-gotcha#comments</comments>
		<pubDate>Thu, 17 May 2012 02:32:21 +0000</pubDate>
		<dc:creator>Jeff Sharp</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[MSDN]]></category>
		<category><![CDATA[Undocumented]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://sharpjs.net/?p=67</guid>
		<description><![CDATA[Most good .NET developers consult the MSDN Library frequently. On the whole, the Library is comprehensive and accurate. Its quality is not quite on the level of UNIX manual pages, but it has improved over the years. Still, one problem persists: too often, the focus is on happy paths, and edge cases are ignored. This [...]]]></description>
			<content:encoded><![CDATA[<p>Most good .NET developers consult the <a href="http://msdn.microsoft.com/en-us/library" target="_blank">MSDN Library</a> frequently.  On the whole, the Library is comprehensive and accurate.  Its quality is not quite on the level of <a href="http://www.freebsd.org/cgi/man.cgi?query=man&#038;manpath=FreeBSD+9.0-RELEASE" target="_blank">UNIX manual pages</a>, but it has improved over the years.  Still, one problem persists: too often, the focus is on happy paths, and edge cases are ignored.  This results in the occasional nasty surprise.</p>
<p>Such was the cause of a recent bug in my client&#8217;s product.  Let&#8217;s examine the Library article on the <a href="http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.movetoroot.aspx" target="_blank"><code>XPathNavigator.MoveToRoot()</code></a> method:</p>
<blockquote><p>Moves the XPathNavigator to the root node that the current node belongs to.</p>
<p>All nodes belong to one and only one document. Therefore, this method is always successful.</p></blockquote>
<p>This <em>seems</em> about as unambiguous as it gets.  Unfortunately, a key detail is missing.  More than one type of node can be considered root, and which one is root in a particular case depends on how the underlying XML document was constructed.  For <code>XPathNavigator</code> instances constructed over <code>XmlDocument</code>s, <code>MoveToRoot()</code> can move to either the owning <code>XmlDocument</code> OR the root <code>XmlElement</code>.</p>
<p>In the following example, the navigator initially is positioned on a child node.  <code>MoveToRoot()</code> moves to the owning <code>XmlDocument</code>.</p>
<p></p><pre class="crayon-plain-tag">[TestMethod]
public void MoveToRoot_MovesToDocument()
{
	var xml   = new XmlDocument();
	var root  = xml.CreateElement(&quot;A&quot;);
	var child = xml.CreateElement(&quot;B&quot;);

	xml.AppendChild(root);
	root.AppendChild(child);

	var nav = child.CreateNavigator();
	nav.MoveToRoot();

	Assert.AreSame(child.OwnerDocument.DocumentElement, root, &quot;DocumentElement&quot;);
	Assert.AreEqual(nav.NodeType, XPathNodeType.Root, &quot;NodeType&quot;);
	Assert.AreSame(nav.UnderlyingObject, xml, &quot;UnderlyingObject&quot;);
}</pre><p></p>
<p>If we omit the <code>AppendChild()</code> call on the document (something commonly done), <code>MoveToRoot()</code> instead moves to the root <code>XmlElement</code>:</p>
<p></p><pre class="crayon-plain-tag">[TestMethod]
public void MoveToRoot_MovesToRootNode()
{
	var xml   = new XmlDocument();
	var root  = xml.CreateElement(&quot;A&quot;);
	var child = xml.CreateElement(&quot;B&quot;);

	//xml.AppendChild(root); // This is the difference!
	root.AppendChild(child);

	var nav = child.CreateNavigator();
	nav.MoveToRoot();

	Assert.IsNull(child.OwnerDocument.DocumentElement, &quot;DocumentElement&quot;);
	Assert.AreEqual(nav.NodeType, XPathNodeType.Element, &quot;NodeType&quot;);
	Assert.AreSame(nav.UnderlyingObject, root, &quot;UnderlyingObject&quot;);
}</pre><p></p>
<p>It&#8217;s understandable why this quirk is not documented.  <code>XPathNavigator</code> is an abstract class.  It has no affinity towards any of the various XML APIs in the Base Class Library.  The framework provides a number of concrete subclasses that adapt <code>XPathNavigator</code> to each API.  Ideally, per-API edge cases would be documented in the MSDN Library article for each concrete subclass.  Those classes, however, aren&#8217;t public.  Since there&#8217;s no good place to document the quirks, this quirk isn&#8217;t documented.</p>]]></content:encoded>
			<wfw:commentRss>http://sharpjs.net/2012/05/16/xpathnavigator-movetoroot-gotcha/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Escape Curly Braces with DebuggerDisplayAttribute</title>
		<link>http://sharpjs.net/2012/04/21/how-to-escape-curly-braces-with-debuggerdisplayattribute</link>
		<comments>http://sharpjs.net/2012/04/21/how-to-escape-curly-braces-with-debuggerdisplayattribute#comments</comments>
		<pubDate>Sun, 22 Apr 2012 03:18:46 +0000</pubDate>
		<dc:creator>Jeff Sharp</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Debugger]]></category>
		<category><![CDATA[Undocumented]]></category>

		<guid isPermaLink="false">http://sharpjs.net/?p=17</guid>
		<description><![CDATA[There are a zillion posts out there that describe how to use DebuggerDisplayAttribute. They all say about the same thing: put property names and other expressions inside curly braces. Like this: [crayon-4fb7a02c5459c/] Voilà! The debugger experience has just been enriched. So curly braces are special. That&#8217;s great, but what if you want to include an [...]]]></description>
			<content:encoded><![CDATA[<p>There are a zillion posts out there that describe how to use <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx" target="_blank"><code>DebuggerDisplayAttribute</code></a>.  They all say about the same thing: put property names and other expressions inside curly braces.  Like this:</p>
<p></p><pre class="crayon-plain-tag">[DebuggerDisplay(&quot;Name = {Name}, Value = {Value}&quot;)]
public class AnyClass
{
    // ...
}</pre><p></p>
<p>Voilà!  The debugger experience has just been enriched.</p>
<p>So curly braces are special.  That&#8217;s great, but what if you want to include an actual curly brace in the display?  Some of the .NET Base Class Library&#8217;s own types display in the debugger with curly braces – <code>System.Type</code>, for example.  Unfortunately, most articles (including <a href="http://msdn.microsoft.com/en-us/library/x810d419.aspx" target="_blank">MSDN</a>, as of this writing) don&#8217;t explain how to accomplish this.</p>
<p>To display a curly brace, just escape it with a backslash:</p>
<p></p><pre class="crayon-plain-tag">[DebuggerDisplay(@&quot;\{Name = {Name}, Value = {Value}\}&quot;)]
public class AnyClass
{
    // ...
}</pre><p> </p>
<p>Notice that the template string now is a verbatim string literal (<code>@"..."</code>).  That is because we do <em>not</em> want the compiler to interpret <code>\{</code> and <code>\}</code> as escape sequences.  Neither are valid C# escape sequences, and your code won&#8217;t build if you try to use them as such.  Rather, <code>\{</code> and <code>\}</code> are interpreted by the debugger at runtime.  Thus, the backslashes themselves need to be compiled into the assembly verbatim.</p>
<p>In summary:</p>
<ul>
<li>Use <code>\{</code> and <code>\}</code> to display curly braces when using <code>DebuggerDisplayAttribute</code></li>
<li>Use <code>@"..."</code> string literals if you want to use <code>\{</code> or <code>\}</code>.</li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://sharpjs.net/2012/04/21/how-to-escape-curly-braces-with-debuggerdisplayattribute/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>This Blog Exists</title>
		<link>http://sharpjs.net/2012/04/17/this-blog-exists</link>
		<comments>http://sharpjs.net/2012/04/17/this-blog-exists#comments</comments>
		<pubDate>Tue, 17 Apr 2012 22:49:23 +0000</pubDate>
		<dc:creator>Jeff Sharp</dc:creator>
				<category><![CDATA[Meta]]></category>

		<guid isPermaLink="false">http://sharpjs.net/?p=1</guid>
		<description><![CDATA[This blog exists.  Now, if only there were some content here, eh?]]></description>
			<content:encoded><![CDATA[<p>This blog exists.  Now, if only there were some content here, eh?</p>]]></content:encoded>
			<wfw:commentRss>http://sharpjs.net/2012/04/17/this-blog-exists/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

