<?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>Software Development &#187; NDFD</title>
	<atom:link href="http://eclectrics.com/software/tag/ndfd/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclectrics.com/software</link>
	<description></description>
	<lastBuildDate>Tue, 15 Sep 2009 20:33:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting the Current Weather Conditions</title>
		<link>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/</link>
		<comments>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 17:17:47 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[BackgroundWorker]]></category>
		<category><![CDATA[Current weather conditions]]></category>
		<category><![CDATA[IsolatedStorage]]></category>
		<category><![CDATA[NDFD]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/?p=170</guid>
		<description><![CDATA[Unlike weather forecasts which are available for any point in the US, current conditions are only available for specific spots where there are reporting stations. The good news is that there are more than 2000 reporting stations, so it&#8217;s not too hard to find one close to you. In this installment I am going to [...]]]></description>
			<content:encoded><![CDATA[<p>Unlike weather forecasts which are available for any point in the US, current conditions are only available for specific spots where there are reporting stations.  The good news is that there are more than 2000 reporting stations, so it&#8217;s not too hard to find one close to you.  In this installment I am going to add the ability to get the current weather conditions to our library of weather tools. (If you are following along in this series, you will notice I have done a bit of mild refactoring, getting ready for the next steps – a local Windows &#8220;weather&#8221; service and a real desktop UI).
</p>
<p>As usual, the folks at the National Weather Service have made our job pretty easy.  You can take a look at what they&#8217;ve given us to work with <a href="http://www.weather.gov/xml/current_obs/">here</a>.  Each observation stations get its own web page containing an Xml document (updated hourly) describing the current weather conditions.  The URL for the page containing the current conditions for each weather station is formed by appending the station&#8217;s identification string to the base URL for current observations.  So for example to get the current conditions for Smithfield, Rhode Island the URL would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://www.weather.gov/xml/current_obs/KSFZ.xml</pre></div></div>

<p>So now the puzzle comes down to how do we find the ID for the reporting station closest to the spot for which we want the current conditions?  We&#8217;ve got a few choices, but all of them start with this <a href="http://www.weather.gov/xml/current_obs/index.xml">document</a>, the index of reporting stations.  It contains an entry for every reporting station, formatted just like this excerpt for our Smithfield station:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>KSFZ<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RI<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Smithfield<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>41.91<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-71.4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.noaa.gov/weather/current/KSFZ.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/KSFZ.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/KSFZ.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 </pre></div></div>

<p>The key pieces here are the <span style="color:#365f91; font-family:Courier New; font-size:9pt">station_name</span>, <span style="color:#365f91; font-family:Courier New; font-size:9pt">state</span> and <span style="color:#365f91; font-family:Courier New; font-size:9pt">station_id</span>.  We&#8217;re also going to find the <span style="color:#365f91; font-family:Courier New; font-size:9pt">latitude</span> and <span style="color:#365f91; font-family:Courier New; font-size:9pt">longitude</span> useful a bit later.  Since this is a reasonably large document (900kb) and we want to play really nice with the NWS folks so they&#8217;ll let us continue to use their toys (and we want our app to be responsive), we&#8217;ll download it once (using a BackgroundWorker) and cache a copy in IsolatedStorage (along with an option to refresh our cache when needed).
</p>
<p>So armed with the list of all the reporting stations, there are a couple of routes we could take.  On the one hand we could simply mimic what the NWS does on their current conditions web page:
</p>
<ol>
<li>Show the user a list of all the states and ask them to pick one.
</li>
<li>Based on their selection, show them a list of all the observation stations in that state and ask them to pick one.
</li>
<li>From their selection, compose the URL for the current conditions page and submit a web request.
</li>
</ol>
<p>The TestFromStateStation sample application (available for download at the end of this post) implements this approach.
</p>
<p>The other approach is to:
</p>
<ol>
<li>Ask the user for the Zip Code for their location.
</li>
<li>Query the NDFD web service for a location (latitude, longitude) associated with the zip code.
</li>
<li>Compute the distance between the zip location and the location of each observation station in the reporting stations index (see above) to find the closest station.
</li>
<li>Use the station Id from the closest station to compose the URL for the current conditions page and submit a web request.
</li>
</ol>
<p>This technique relies on being able to determine the distance between two points on the earth&#8217;s surface. There are a number of techniques we can use – you can read a really good description of your options thanks to the folks at <a href="http://www.movable-type.co.uk/scripts/latlong.html">movable-type</a>.  For our purposes, the spherical law of cosines is good enough (see ObservationStations.GetDistance() in the NationalWeatherServiceLibrary project in the attached solution).  The TestFromZip sample (see below) implements this approach.
</p>
<p>So there you have it, the ability to get the current weather conditions using either state/station or zip code.  As promised, here are the downloads: <a href="http://eclectrics.com/software/wp-content/uploads/2009/09/TestFromStateStation.zip">TestFromStateStation.exe</a>, <a href="http://eclectrics.com/software/wp-content/uploads/2009/09/TestFromZip.zip">TestFromZip.exe</a> and the <a href="http://eclectrics.com/software/wp-content/uploads/2009/09/WeatherTools2.zip">complete VS2008 solution</a>.
</p>
<p>At this point we&#8217;re done with the web service pieces of the project; we know how to get forecasts and current conditions for anywhere in the US.  Next it&#8217;s time to create the UI.
</p>
<p>
 </p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XmlTreeView – A Custom Painted TreeView</title>
		<link>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/</link>
		<comments>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 14:45:39 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Custom Paint TreeView]]></category>
		<category><![CDATA[ISO-8859-1]]></category>
		<category><![CDATA[NDFD]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/</guid>
		<description><![CDATA[Last time I described how to build a WCF client for the NDFD web service. The gist was we needed to create a custom TextMessageEncoder that could handle the ISO-8859-1 encoding used by the service. With that done, it&#8217;s time to start thinking about how to use all the data to which we now have [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I described how to build a WCF client for the NDFD web service. The gist was we needed to create a custom <span style="color:#365f91; font-family:Consolas; font-size:9pt">TextMessageEncoder</span> that could handle the ISO-8859-1 encoding used by the service. With that done, it&#8217;s time to start thinking about how to use all the data to which we now have access. My goal over the next week or so is to convert the NDFD client into its own local WCF service, hosted in a Windows service, to add the ability to get current weather conditions, and finally to build a couple of desktop clients (a desktop Gadget and an ActiveX control) that use the service.</p>
<p>But before I jump into that, I&#8217;m going to take a small detour and talk about custom painting a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. This is motivated by the fact that the NDFD Xml data is a bit hard to visually parse when displayed as raw Xml, so I thought I&#8217;d simplify my debugging a bit by pushing the data into a custom painted <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>.</p>
<p>There are a couple approaches to custom painting in a managed <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. The simplest (OwnerDraw) is to set the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView.DrawMode</span> property like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DrawMode</span> <span style="color: #008000;">=</span> TreeViewDrawMode.<span style="color: #0000FF;">OwnerDrawText</span><span style="color: #008000;">;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DrawMode</span> <span style="color: #008000;">=</span> TreeViewDrawMode.<span style="color: #0000FF;">OwnerDrawAll</span><span style="color: #008000;">;</span></pre></div></div>

<p>Then you override <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> and do your painting. The big benefit with this approach is that the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> does most of the heavy lifting – it determines when a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNode</span> needs redrawn as well as the bounding rectangle – all you need to do is draw the contents of a node when requested.</p>
<p>The problem with this approach is that as soon as your node content gets a bit complex, you start down a slippery slope of increasing complexity as you add hit test code for selection highlighting etc. And if your drawing code gets the least bit complex, you will start to notice redrawing flicker as you resize windows containing your custom painted <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. (It&#8217;s unfortunate, but the built-in double buffering does not work for <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawText</span> or <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawAll</span>. You can get some improvement in the flicker by overriding <span style="color:#365f91; font-family:Consolas; font-size:9pt">WndProc</span> and eating all the <span style="color:#365f91; font-family:Consolas; font-size:9pt">WM_ERASEBKGND</span> messages, but it doesn&#8217;t really fix the problem.) So, except for the simplest situations, I usually end up using the second approach which is to do all the painting myself (UserPaint). It&#8217;s a little complex the first time, but most of the complexity is boilerplate code that you can reuse (the attached code is a pretty good starting point).</p>
<p>You enable user painting by setting several of the <span style="color:#365f91; font-family:Consolas; font-size:9pt">ControlStyles</span> bits for your <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">UserPaint</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">AllPaintingInWmPaint</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">OptimizedDoubleBuffer</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now only <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> and <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span> get called (<span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> is out of the loop), and this is where the fun begins.</p>
<p>For starters, you&#8217;ll want to override both <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> and <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span>. For <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span>, I find it&#8217;s usually most efficient to do nothing more than insure the base class <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span> isn&#8217;t called:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnPaintBackground<span style="color: #000000;">&#40;</span>PaintEventArgs pevent<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// We can do a better job of predicting where nodes will paint the background</span>
	<span style="color: #008080; font-style: italic;">// and what area is left, so just disable background painting.</span>
	<span style="color: #008080; font-style: italic;">//base.OnPaintBackground(pevent);</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This means in <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span>, we need to paint not only the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span> but the empty area where there are no <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span> as well. So <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnPaint<span style="color: #000000;">&#40;</span>PaintEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// If we have nodes, then we have to draw them - and that means everything</span>
	<span style="color: #008080; font-style: italic;">// about the node.</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Nodes</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// If we have nodes then there has to be a Nodes[0]. Use it to get the</span>
		<span style="color: #008080; font-style: italic;">// height of one TreeNode - in a TreeView, all nodes are the same height</span>
		<span style="color: #008080; font-style: italic;">// (unless you are using PInvoke to call TVM_SETITEM and set TV_ITEM </span>
		<span style="color: #008080; font-style: italic;">// iIntegral member for a specific node - which we are not doing!).</span>
		<span style="color: #FF0000;">int</span> nodeHeight <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Nodes</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Bounds</span>.<span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #FF0000;">int</span> curY <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Walk down the client area, stepping by the height of one row, looking </span>
		<span style="color: #008080; font-style: italic;">// for nodes.</span>
		<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>curY <span style="color: #008000;">&lt;=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span>.<span style="color: #0000FF;">Height</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// This probably isn't the most efficient way to do this, but</span>
			<span style="color: #008080; font-style: italic;">// at least we aren't writing our own code to walk the tree...</span>
			TreeNode node <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetNodeAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, curY<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// If this rect does contain a node, draw the node.</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				DrawTreeNodeEventArgs args <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DrawTreeNodeEventArgs<span style="color: #000000;">&#40;</span>
					e.<span style="color: #0000FF;">Graphics</span>,
					node,
					e.<span style="color: #0000FF;">ClipRectangle</span>,
					GetNodeStates<span style="color: #000000;">&#40;</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// manufacture the proper TreeNodeStates</span>
&nbsp;
				DrawCustomNode<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// If this rect doesn't contain a node, we need to fill it with the </span>
			<span style="color: #008080; font-style: italic;">// background color.</span>
			<span style="color: #0600FF;">else</span>
				e.<span style="color: #0000FF;">Graphics</span>.<span style="color: #0000FF;">FillRectangle</span><span style="color: #000000;">&#40;</span>
					_backBrush, 
					<span style="color: #FF0000;">0</span>, 
					curY, 
					<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span>.<span style="color: #0000FF;">Width</span>, 
					nodeHeight<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			curY <span style="color: #008000;">+=</span> nodeHeight<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// If there are no nodes in the tree, we're going to have to paint the </span>
	<span style="color: #008080; font-style: italic;">// entire background.</span>
	<span style="color: #0600FF;">else</span>
		e.<span style="color: #0000FF;">Graphics</span>.<span style="color: #0000FF;">FillRectangle</span><span style="color: #000000;">&#40;</span>_backBrush, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Don't need - in this case we can guarantee no one is listening...</span>
	<span style="color: #008080; font-style: italic;">//base.OnPaint(e);</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, our job is to just walk down the client area of the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>, one node height at a time, checking to see if there is a node there. (There is a lot of optimizing opportunity here.) If we have a node we draw it; if there isn&#8217;t, we draw background. That&#8217;s all there is to it &#8211; well almost. You probably noticed there are a couple of methods stuck in the middle of <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> that we haven&#8217;t talked about yet – and these can be a bit of a pickle.</p>
<p>The first problem we need to resolve is addressed by the <span style="color:#365f91; font-family:Consolas; font-size:9pt">GetNodeStates</span> method which supplies the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodeStates</span> parameter for the <span style="color:#365f91; font-family:Consolas; font-size:9pt">DrawTreeNodeEventArgs</span> constructor. The stock <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> that you are familiar with has a curious behavior when it comes to highlighting selected <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span>. If you click on one node to select it, its background gets painted with <span style="color:#365f91; font-family:Consolas; font-size:9pt">SystemBrushes.Highlight</span> and all is good. But if now you click-and-hold on a second node you will notice the first node loses its highlight and the second node is now highlighted – but it gets even more complex. If now (while still holding your mouse button down) you move your mouse a bit (say more than 15 pixels or so) the highlight reverts to the first node. This behavior is made possible by <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodeStates.Focused</span>. It turns out that in a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>, node highlighting is indicated by <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> rather than <span style="color:#365f91; font-family:Consolas; font-size:9pt">Selected</span>, so it&#8217;s the coming and going of <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> for the first and second nodes that is prompting the highlighting behavior we just saw. The problem is that we don&#8217;t actually have a way to directly test for the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state. If you are using <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawText</span> or <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawAll</span> you get the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state handed to you when your <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> override is called, but when doing our own drawing in <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span>, we need to manufacture the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state and this is the purpose for <span style="color:#365f91; font-family:Consolas; font-size:9pt">GetNodeStates </span>(this is the secret sauce):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> TreeNodeStates GetNodeStates<span style="color: #000000;">&#40;</span>TreeNode node<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	TreeNodeStates states <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// If mouse button is not down, then normal rules apply - if a node is</span>
	<span style="color: #008080; font-style: italic;">// selected, then it also has focus. We set _mouseDownNode in our</span>
	<span style="color: #008080; font-style: italic;">// OnMouseDown override and clear it in our OnMouseUp override.</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_mouseDownNode <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node.<span style="color: #0000FF;">IsSelected</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Selected</span> <span style="color: #008000;">|</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// But if a mouse button has been clicked on a node, then we need to check</span>
	<span style="color: #008080; font-style: italic;">// to see if that node is currently drop-highlighted.</span>
	<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">!=</span> _mouseDownNode<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// If this is the currently selected node, but a mouse button has been</span>
		<span style="color: #008080; font-style: italic;">// clicked-and-held on another node and that node is currently showing </span>
		<span style="color: #008080; font-style: italic;">// the drop-highlight, then we don't show selection highlighting for this</span>
		<span style="color: #008080; font-style: italic;">// node.</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>IsDropHighlighted<span style="color: #000000;">&#40;</span>_mouseDownNode<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> node.<span style="color: #0000FF;">IsSelected</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Selected</span> <span style="color: #008000;">|</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Othewise if this is the node that has been clicked-and-held and if</span>
	<span style="color: #008080; font-style: italic;">// this node is currently showing drop-highlighting, then show it. We </span>
	<span style="color: #008080; font-style: italic;">// still need to check to see if drop-highlighting is being shown for </span>
	<span style="color: #008080; font-style: italic;">// this node because if the user clicked-and-held on this node, but</span>
	<span style="color: #008080; font-style: italic;">// then started to move the mouse, drop-highlighting is removed by</span>
	<span style="color: #008080; font-style: italic;">// the underlying Win32 tree control.</span>
	<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">==</span> _mouseDownNode<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsDropHighlighted<span style="color: #000000;">&#40;</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> states<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The final pieces here are a couple of calls to the underlying Win32 tree control to get the state of the nodes in question from which we can determine if a particular node is drop-highlighted:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsDropHighlighted<span style="color: #000000;">&#40;</span>TreeNode node<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> MASK <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">TVIF_STATE</span> <span style="color: #008000;">|</span> NativeMethods.<span style="color: #0000FF;">TVIF_HANDLE</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// TV_ITEM stateMask doesn't seem to matter when reading state - we get</span>
	<span style="color: #008080; font-style: italic;">// all the state bits anyway (Windows7).</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> STATEMASK <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">TVIS_DROPHILITED</span><span style="color: #008000;">;</span>
&nbsp;
	NativeMethods.<span style="color: #0000FF;">TV_ITEM</span> lParam <span style="color: #008000;">=</span> 
		<span style="color: #008000;">new</span> NativeMethods.<span style="color: #0000FF;">TV_ITEM</span> <span style="color: #000000;">&#123;</span> 
			hItem <span style="color: #008000;">=</span> node.<span style="color: #0000FF;">Handle</span>, 
			mask <span style="color: #008000;">=</span> MASK, 
			stateMask <span style="color: #008000;">=</span> STATEMASK <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	UnsafeNativeMethods.<span style="color: #0000FF;">SendMessage</span><span style="color: #000000;">&#40;</span>
		<span style="color: #008000;">new</span> HandleRef<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Handle</span><span style="color: #000000;">&#41;</span>, 
		NativeMethods.<span style="color: #0000FF;">TVM_GETITEMW</span>, 
		<span style="color: #FF0000;">0</span>, 
		<span style="color: #0600FF;">ref</span> lParam<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>lParam.<span style="color: #0000FF;">state</span> <span style="color: #008000;">&amp;</span> NativeMethods.<span style="color: #0000FF;">TVIS_DROPHILITED</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>(The definitions for the various Win32 methods, structures and constants can be found in the attached sample.)</p>
<p>The second method we haven&#8217;t talked about is DrawCustomNode – this is where I actually get around to drawing the TreeNodes. Since my goal for this project was to create a simple view of an XmlDocument, the code just implements the stylized drawing to do that – and this is where you would put your code to draw a BarGraphTreeNode or whatever. In my case, since I am &#8220;syntax coloring&#8221; Xml, there is quite a bit going on to insure various segments of the colored text line up correctly – but that&#8217;s another story.</p>
<p>The attached sample (<a href="http://eclectrics.com/software/wp-content/uploads/2009/08/WeatherTools-Tester.zip">executable</a>, <a href="http://eclectrics.com/software/wp-content/uploads/2009/08/WeatherTools.zip">source</a>) contains a fully working test app using the NDFD web service to get weather forecasts for specific ZIP codes. It pushes the Xml forecast data into our new XmlTreeView. I hope you enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Consuming NDFD Web Services from a C# Client</title>
		<link>http://eclectrics.com/software/2009/08/consuming-ndfd-web-services-from-a-c-client/</link>
		<comments>http://eclectrics.com/software/2009/08/consuming-ndfd-web-services-from-a-c-client/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 17:01:36 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ISO-8859-1]]></category>
		<category><![CDATA[NDFD]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/?p=130</guid>
		<description><![CDATA[I like to know what the weather is going to be &#8211; it sets the tone for the day. But it is getting more difficult to get access to weather forecasts from the local television stations. Clearly lots of folks must tune in just for the weather because there seem to be this cat-and-mouse game [...]]]></description>
			<content:encoded><![CDATA[<p>I like to know what the weather is going to be &#8211; it sets the tone for the day. But it is getting more difficult to get access to weather forecasts from the local television stations. Clearly lots of folks must tune in just for the weather because there seem to be this cat-and-mouse game where they parcel out snippets of info with &#8220;My complete forecast&#8221; always happening some time later…</p>
<p><img src="http://eclectrics.com/software/wp-content/uploads/2009/08/081909_1701_ConsumingND1.png" alt="" align="left" />National Weather Service to the rescue. They have a nice web site where you can get your <a href="http://forecast.weather.gov/MapClick.php?minlon=-125&amp;maxlon=-120.8&amp;minlat=43.39&amp;maxlat=46.91&amp;mapwidth=354&amp;site=pqr&amp;map.x=170&amp;map.y=159">local 7-day forecast</a> (updated hourly). But what&#8217;s even more interesting, if you click around on the map you can get a forecast for anywhere in the US, even for places that aren&#8217;t even close to a NWS field office (this keeps getting better and better) and all this data is available from a <a href="http://www.weather.gov/xml/">SOAP-based web service</a>, the National Digital Forecast Database (NDFD). So it looks like there is a way to get weather forecasts– and I don&#8217;t have to wait for the &#8220;News at 5&#8243;. And I can stick them in a desktop Gadget, in a Word document or on a PowerPoint slide.</p>
<p>The NWS provides a <a href="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl">WSDL document</a> so you might think it would be a simple thing to add a Service Reference to your project, have a proxy automatically generated, and you&#8217;re ready to roll. But unfortunately there is a wrinkle. The app.config file that&#8217;s auto generated from the NDFD WSDL document specifies that we&#8217;re going to use the stock basicHttpBinding and every time you send a SOAP request to the service you see this: <span style="color:#365f91; font-family:Courier New; font-size:9pt"><br />
</span></p>
<p><span style="color:#365f91; font-family:Courier New; font-size:9pt">System.ServiceModel.ProtocolException was unhandled. Message=&#8221;The content type text/xml; charset=ISO-8859-1 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly.</span></p>
<p>In this case, the problem really doesn&#8217;t have anything to do with correctly implementing IsContentTypeSupported. It&#8217;s caused by the fact that the NDFD service uses ISO-8859-1 encoding. However the MessageEncoder used by the basicHttpBinding only understands UTF-8, UTF-16 and Big-Endian Unicode… So finally there&#8217;s an excuse to create a custom binding. And the good news is that although this might be tedious, it&#8217;s not hard and there is a Microsoft <a href="http://msdn.microsoft.com/en-us/library/ms751486(VS.85).aspx">sample</a> that does exactly what we need. Instead of using the &#8220;optimized&#8221; XML reader and writer used by WCF, the sample uses a plain ole System.Xml.XmlReader and System.Xml.XmlWriter which do support ISO-8859-1. So aside from having to add a custom MessageEncoder, MessageEncoderFactory, MessageEncodingBindingElement and BindingElementExtensionElement to our project (just cut and paste from the Microsoft sample), things really aren&#8217;t too bad.</p>
<p>Once the custom MessageEncoder (and friends) are in place we still need to modify the auto-generated system.ServiceModel section of our app.config file (actually, in this case we&#8217;re going to just replace the whole section). Here is what it looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NdfdBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StmEncoding</span> <span style="color: #000066;">messageVersion</span>=<span style="color: #ff0000;">&quot;Soap11&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpTransport</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;customBinding&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;NdfdBinding&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;NdfdServiceReference.ndfdXMLPortType&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ndfdXMLPort&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindingElementExtensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;StmEncoding&quot;</span></span>
<span style="color: #009900;">                 <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;StmEncoderLibrary.StmEncoderBindingElementExtensionElement, StmEncoderLibrary&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindingElementExtensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.serviceModel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The first thing required is a new endpoint.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;endpoint</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLserver.php&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;customBinding&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">bindingConfiguration</span>=<span style="color: #ff0000;">&quot;NdfdBinding&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">contract</span>=<span style="color: #ff0000;">&quot;NdfdServiceReference.ndfdXMLPortType&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ndfdXMLPort&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/client<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The address and contract are the same ones that were auto-generated from the NDFD WSDL document, but instead of using a basicHttpBinding, we specify a customBinding which gives us complete control of the WCF messaging stack. The specific custom binding we&#8217;re going to use in this case is named &#8220;NdfdBinding&#8221;. The name of the endpoint is how we are going to reference this endpoint when we create the service proxy in our application.</p>
<p>The next piece is the customBinding.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;binding</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;NdfdBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StmEncoding</span> <span style="color: #000066;">messageVersion</span>=<span style="color: #ff0000;">&quot;Soap11&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpTransport</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/binding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customBinding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You probably don&#8217;t recognize the StmEncoding element (neither does Visual Studio IntelliSense). It&#8217;s a custom element that references a binding extension – this is how our custom MessageEncoder implementation gets into the act. Note that StmEncoding has messageVersion and encoding attributes – this is where we specify now our encoder is going to work – they are the values that eventually get passed to our custom MessageEncoderFactory and then to our custom MessageEncoder constructor.</p>
<p>Finally the bindingExtension:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bindingElementExtensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;StmEncoding&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;StmEncoderLibrary.StmEncoderBindingElementExtensionElement, StmEncoderLibrary&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bindingElementExtensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/extensions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This is where we specify the fully-qualified name of our class derived from BindingElementExtensionElement and the assembly containing the entire custom message encoder implementation.</p>
<p>Well, that&#8217;s it. Once you have the custom MessageEncoder that handles ISO-8859-1 and an appropriately constructed app.config file you should be able to talk to the NDFD web service and get a weather forecast whenever you want.</p>
<p>If you would like try out the code, <a href="http://eclectrics.com/software/wp-content/uploads/2009/08/NdfdTest-Application.zip">here </a>is a working version of a Windows Forms application and a dll containing a custom MessageEncoder, and <a href="http://eclectrics.com/software/wp-content/uploads/2009/08/NdfdForecast.zip">here </a>is the source code.</p>
<p>Next I think I&#8217;ll make the XML data a bit more readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2009/08/consuming-ndfd-web-services-from-a-c-client/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
