<?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>Cameron McKay &#187; xml</title>
	<atom:link href="http://cdmckay.org/blog/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming, Politics and Game Design</description>
	<lastBuildDate>Tue, 24 Aug 2010 13:57:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Consuming a J2EE Web Service with PHP SOAP</title>
		<link>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/</link>
		<comments>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 22:39:59 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[nusoap]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=303</guid>
		<description><![CDATA[I recently had to do some work for a company which involved using PHP to consume a J2EE-based WSDL web service.  The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected.

What is WSDL?
Before we start, let&#8217;s talk a bit about what WSDL is. [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to do some work for a company which involved using <a href="http://www.php.net">PHP</a> to consume a <a href="http://java.sun.com/javaee/">J2EE</a>-based <a href="http://www.w3.org/TR/wsdl">WSDL</a> web service.  The project was fairly straightforward, involving only a one-way call to the web service, i.e. no result needed to be collected.</p>
<p><span id="more-303"></span></p>
<h3>What is WSDL?</h3>
<p>Before we start, let&#8217;s talk a bit about what <a href="http://en.wikipedia.org/wiki/Web_Services_Description_Language">WSDL</a> is.  <em>(Warning: I am very new to WSDL, SOAP and web services in general, so feel free to correct me if I get any of these details wrong).</em></p>
<p>WSDL stands for Web Service Description Language.  As the name suggests, it is an XML language used for describing web services.  By describe, I mean a WSDL file contains information like the functions that a client can call, and data types it uses, and what exceptions it might throw.  This is useful for clients that might contact the web service.  Exactly why that information is useful will become evident in examples below.</p>
<h3>What is SOAP?</h3>
<p>At one point, a long time ago, in a web far far away, <a href="http://en.wikipedia.org/wiki/SOAP_(protocol)">SOAP</a> stood for Simple Object Access Protocol.  Now it stands for nothing.  SOAP is a protocol for exchanging structured information on the web.  Like WSDL, SOAP uses XML.  In fact, SOAP is a protocol often used to exchange WSDL information, although one can also use straight HTTP to do it as well.</p>
<p>All we really need to know (right now) is that a SOAP library permits us to communicate with a WSDL web service that has SOAP bindings (which would be almost any we would encounter in the wild).</p>
<h3>Which library/module to use?</h3>
<p>In order to determine which library to use, I did some googling.  What I found were two options: the <a href="http://sourceforge.net/projects/nusoap/">NuSOAP</a> library and the <a href="http://ca.php.net/manual/en/book.soap.php">PHP SOAP</a> module.</p>
<p>So which one should we use? I initially leaned towards NuSOAP due to the fact that it was an external library, and thus would probably work with almost any PHP installation.  Unfortunately, after perusing the NuSOAP Sourceforge website, I noticed the library hadn&#8217;t been updated since 2005 (a bad sign).  In fact, after further reading, I discovered that, as of PHP 5, the NuSOAP namespace also conflicted with the PHP SOAP module namespace (and hadn&#8217;t yet been fixed).  As a result, I decided to go with the PHP SOAP module.</p>
<h3>The Code</h3>
<p>The first thing anyone needs to do when consuming a WSDL service with PHP is to create a new client object.  By creating a client object, PHP basically reads the server&#8217;s WSDL file and determines which functions are available to us.  A SoapClient is created using the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://path/to/webservice.wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what&#8217;s going on at line 7?  Those two features are basically used to make the SoapClient act as we&#8217;d expect.  For example, if you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then you&#8217;ll find you get a lot of casting exceptions when dealing with a Java-based webservice that uses any complex data structures.  If you don&#8217;t include the SOAP_USE_XSI_ARRAY_TYPE feature, then PHP will turn 1-element arrays return types into objects, which is not what you want 90% of the time and often gives confusing results.</p>
<p>Now let&#8217;s actually consume a web service.  Let&#8217;s pretend we&#8217;re using the J2EE service provided by this <a href="http://java.sun.com/developer/technicalArticles/J2EE/j2ee_ws/">article</a>.  Here are the contents of the WSDL file from that article:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="xml" style="font-family:Consolas, 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;definitions</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span> <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span> <span style="color: #000066;">xmlns:tns</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/&quot;</span> <span style="color: #000066;">xmlns:xsd</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:soap</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;types</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;int_2&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;part</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;result&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:int&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span> <span style="color: #000066;">parameterOrder</span>=<span style="color: #ff0000;">&quot;int_1 int_2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_add&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:MathFace_addResponse&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/portType<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;MathFaceBinding&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;tns:MathFace&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:binding</span> <span style="color: #000066;">transport</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/http&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;rpc&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:operation</span> <span style="color: #000066;">soapAction</span>=<span style="color: #ff0000;">&quot;&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/input<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:body</span> <span style="color: #000066;">encodingStyle</span>=<span style="color: #ff0000;">&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;</span> </span>
<span style="color: #009900;">		    <span style="color: #000066;">use</span>=<span style="color: #ff0000;">&quot;encoded&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;urn:Foo&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/output<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<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 style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyFirstService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;port</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MathFacePort&quot;</span> <span style="color: #000066;">binding</span>=<span style="color: #ff0000;">&quot;tns:MathFaceBinding&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;soap:address</span> <span style="color: #000066;">location</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/math-service/math&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/port<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/definitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>What can we tell about the web service by looking at this file?  Well the first thing you should notice is lines 12-14.  In those lines, the web service defines an operation (&#8220;add&#8221;) that takes two arguments (&#8220;int_1 int_2&#8243;).  As a result, we know that the web service exposes an add operation that takes two integer arguments.  Let&#8217;s use that knowledge to invoke the add operation in PHP using the SOAP module:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:Consolas, monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #666666; font-style: italic;">// The path to the WSDL web service.</span>
  <span style="color: #000088;">$wsdl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:8080/math-service/math?wsdl&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// The connection options.</span>
  <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'features'</span> <span style="color: #339933;">=&gt;</span> 
    SOAP_USE_XSI_ARRAY_TYPE <span style="color: #339933;">+</span> SOAP_SINGLE_ELEMENT_ARRAYS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Create a new SoapClient.</span>
  <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SoapClient<span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsdl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">echo</span> <span style="color: #0000ff;">&quot;SoapClient creation successful&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// Call the add operation.</span>
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// This should print 4.</span>
  <span style="color: #990000;">echo</span> <span style="color: #000088;">$response</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>How&#8217;d the PHP object $client know to have an add method?  It simply read the WSDL file the same way we did, and realized that the web service exposed that operation.</p>
<p>As you can see, consuming a J2EE web service from a PHP client is not too complicated.  Once you understand the rudiments of a WSDL file and the PHP SOAP module, calling remote functions becomes a snap.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/03/05/consuming-a-j2ee-web-service-with-php-soap/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating an HTML document with .NET</title>
		<link>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/</link>
		<comments>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 08:15:09 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=43</guid>
		<description><![CDATA[Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document.  This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format.

The String Concatenation Approach
Whatever your reason is for generating HTML, the best bet is your first prototype looked [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when you&#8217;re programming, you&#8217;ll want to generate an HTML document.  This may be because you&#8217;re writing some sort of web app, or maybe you want your program to be able to output in a web-friendly format.</p>
<p><span id="more-43"></span></p>
<h3>The String Concatenation Approach</h3>
<p>Whatever your reason is for generating HTML, the best bet is your first prototype looked something like this:</p>
<p><em>(Aside: All this code is in VB.NET&#8230; the original VB.NET (i.e. not 2005 or later).  It also assumes ASP.NET 1.1.  This is because the client I work for still uses this technology, so all my experience is with it.  The code should still be fine though, as all versions of .NET after 1.1 are more or less backwards compatible.)</em></p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:Consolas, monospace;"><span style="color: #008000;">' Run some query on the database.</span>
<span style="color: #990099; font-weight: bold;">Dim</span> dataTable <span style="color: #330066;">As</span> DataTable <span style="color: #006600; font-weight: bold;">=</span> Query<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Dump it into an HTML document.</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;head&gt;&lt;title&gt;Database Table&lt;/title&gt;&lt;/head&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span>Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' Print the rows.</span>
<span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> row <span style="color: #330066;">As</span> DataRow <span style="color: #990099; font-weight: bold;">In</span> dataTable
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
  <span style="color: #008000;">' Print the cells.</span>
  <span style="color: #990099; font-weight: bold;">For</span> <span style="color: #990099; font-weight: bold;">Each</span> item <span style="color: #330066;">As</span> Object <span style="color: #990099; font-weight: bold;">In</span> row
    <span style="color: #990099; font-weight: bold;">If</span> IsDBNull<span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">False</span> <span style="color: #990099; font-weight: bold;">Then</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&quot;</span> + <span style="color: #990099; font-weight: bold;">CStr</span><span style="color: #006600; font-weight:bold;">&#40;</span>item<span style="color: #006600; font-weight:bold;">&#41;</span> + <span style="color: #cc0000;">&quot;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">Else</span>
      <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">If</span>
  <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
  <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/tr&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">For</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/table&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/body&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;/html&gt;&quot;</span> + Environment.<span style="color: #9900cc;">NewLine</span><span style="color: #006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now don&#8217;t get me wrong, this code will do the trick.  But as you write it, you start to feel a bit dirty.  HTML is a language for representing hierarchies, yet you&#8217;re concatenating strings.  You start to feel like there&#8217;s a more elegant way to do it, a way that takes advantage of the structure of HTML.</p>
<p>And there is.</p>
<h3>The Programmatic Approach</h3>
<p>Ok, so we want to take an approach that has some knowledge of the HTML we&#8217;re writing.  Enter XML.  .NET uses XML for, among other things, it&#8217;s <a href="http://en.wikipedia.org/wiki/Javadoc">Javadoc</a>-like documentation generator and it&#8217;s ASP.NET config files.  With XML so prevalent in .NET, it&#8217;s not surprising to discover that it&#8217;s part of the standard library, in the namespace <code>System.Xml</code>.</p>
<p>So you&#8217;re thinking &#8220;Cameron, who cares about XML, I want to make an HTML document!&#8221;  Gear down, sugar plum, XML can still help!  As you may or may not know, HTML (at least the XHTML dialects) is a reformulation of XML.  That is, an XHTML document is an XML document.  As a result, we can use .NET&#8217;s built-in XML support to generate an HTML document.</p>
<p>Thus, if we want to write the same code in previous example in .NET XML, we would do the following.  First, we would need to make a new XML document.  That&#8217;s easy enough:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Xml</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create a new XML document.</span>
<span style="color: #0600FF;">Dim</span> xmlDoc <span style="color: #FF8000;">As</span> XmlDocument <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> XmlDocument</pre></div></div>

<p>Now we want to start writing some HTML.  So let&#8217;s create our <code>html</code> tag and add some headers.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">' Create the html tag.</span>
<span style="color: #0600FF;">Dim</span> xmlRoot <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;html&quot;</span><span style="color: #000000;">&#41;</span>
xmlDoc.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRoot<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the head tag and append it under the html element.</span>
<span style="color: #0600FF;">Dim</span> xmlHead <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;head&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlHead<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the title tag, set it's text to &quot;Database Table&quot;</span>
<span style="color: #008080; font-style: italic;">' and append it under the head element.</span>
<span style="color: #0600FF;">Dim</span> xmlTitle <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;title&quot;</span><span style="color: #000000;">&#41;</span>
xmlTitle.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">CreateTextNode</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Database Table&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
xmlHead.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTitle<span style="color: #000000;">&#41;</span></pre></div></div>

<p>Wow, that&#8217;s a lot of code.  What&#8217;s going on there?  First, let&#8217;s look at the <code>XmlDocument</code> class.  This is important when we&#8217;re generating XML documents.  It&#8217;s what we use to create new elements.  However, even though it creates new elements, it doesn&#8217;t add them to the document.  Thus, we need to use the <code>AppendChild</code> method of the appropriate element to add it to the document.  As you can see above, we created a root element (<code>html</code>) and added that to the XML document (<code>xmlDoc</code>) first.  Then we created a <code>head</code> element, and added that to the root element, not the <code>xmlDoc</code>.  This was done in order to form a hierarchy, which is, in essence, what a XML (and by extension HTML) document is.</p>
<p>Here is what the resulting XML/XHTML document would look like so far:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:Consolas, monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Database Table<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So far so good, let&#8217;s finish it off.  Here&#8217;s the rest of the code we&#8217;d need to make our simple HTML document:</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:Consolas, monospace;"><span style="color: #008080; font-style: italic;">' Create the body element and append it to the root.</span>
<span style="color: #0600FF;">Dim</span> xmlBody <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;body&quot;</span><span style="color: #000000;">&#41;</span>
xmlRoot.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlBody<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the table and append it.</span>
<span style="color: #0600FF;">Dim</span> xmlTable <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;table&quot;</span><span style="color: #000000;">&#41;</span>
xmlBody.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlTable<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Create the rows.</span>
<span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> row <span style="color: #FF8000;">As</span> DataRow In dataTable
  <span style="color: #0600FF;">Dim</span> xmlRow <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">CreateElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;tr&quot;</span><span style="color: #000000;">&#41;</span>
  xmlTable.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlRow<span style="color: #000000;">&#41;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">' Create the cells.</span>
  <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> item <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span> In row
    <span style="color: #0600FF;">Dim</span> content <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;&quot;</span>
    <span style="color: #0600FF;">If</span> IsDBNull<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">False</span> <span style="color: #FF8000;">Then</span> content <span style="color: #008000;">=</span> <span style="color: #0600FF;">CStr</span><span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">Dim</span> xmlCell <span style="color: #FF8000;">As</span> XmlElement <span style="color: #008000;">=</span> xmlDoc.<span style="color: #0000FF;">createElement</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;td&quot;</span><span style="color: #000000;">&#41;</span>
    xmlCell.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">createTextNode</span><span style="color: #000000;">&#40;</span>content<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    xmlRow.<span style="color: #0000FF;">AppendChild</span><span style="color: #000000;">&#40;</span>xmlCell<span style="color: #000000;">&#41;</span>
  <span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
<span style="color: #0600FF;">End</span> <span style="color: #FF8000;">For</span>
&nbsp;
<span style="color: #008080; font-style: italic;">' Write the XML/HTML document.</span>
Response.<span style="color: #0600FF;">Write</span><span style="color: #000000;">&#40;</span>xmlDoc.<span style="color: #0000FF;">OuterXml</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p>And there you have it: a programmatic way to generate an HTML document.  So why bother?  Here&#8217;s a few reasons:</p>
<p><strong>Safety</strong>.  If you generate your HTML document by concatenating strings, there&#8217;s a chance you might forget to close a tag.  If you generate the document programmatically, you can ensure that all your tags will always be closed.</p>
<p><strong>Maintainability and readability</strong>.  I&#8217;ve had to deal with code where the HTML is generated by concatenation.  It&#8217;s confusing and frustrating.  If you generate your HTML documents programmatically, it&#8217;s a lot easier for a future programmer to follow what&#8217;s going on (especially if you, y&#8217;know, document it).</p>
<p><strong>Reusability and modifiability</strong>.  If you create your HTML document as a flat string, it makes it really hard to modify and reuse elsewhere in your code.  For example, if you wanted to add a tag in the middle of your HTML document using the string concatenation method, you will have to do a lot of painful string manipulations.  With the programmatic approach, it&#8217;s as simple as just calling <code>AppendChild</code> on the appropriate <code>XmlElement</code>.</p>
<p>Using an <code>XmlDocument</code> to represent an XML document just makes sense.  It&#8217;s less crude and more versatile.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/01/creating-an-html-document-with-net/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
