<?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>Garkbit Blog &#187; ASP.NET/C#</title>
	<atom:link href="http://wp.garkbit.com/category/web-development/aspnet-csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://wp.garkbit.com</link>
	<description>... on the other side of the universe</description>
	<lastBuildDate>Mon, 14 Dec 2009 13:29:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Transforming XML/XSLT using .NET 3.5</title>
		<link>http://wp.garkbit.com/2009-12-14/transforming-xmlxslt-using-net-3-5/</link>
		<comments>http://wp.garkbit.com/2009-12-14/transforming-xmlxslt-using-net-3-5/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 13:27:35 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[XML/XSLT]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://wp.garkbit.com/?p=200</guid>
		<description><![CDATA[Here is a simple example of how you can apply a XSLT stylesheet to a XML file and deliver the result to the browser using ASP.NET 3.5.
default.aspx.cs

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public partial class _default : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

        [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple example of how you can apply a XSLT stylesheet to a XML file and deliver the result to the browser using ASP.NET 3.5.</p>
<p><strong>default.aspx.cs</strong></p>
<pre class="brush: csharp;">
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

public partial class _default : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

        // Source paths
        string xmlFilePath = Server.MapPath(&quot;hello-world.xml&quot;);
        string xsltFilePath = Server.MapPath(&quot;hello-world.xslt&quot;);

        // Load XML
        XPathNavigator nav = new XPathDocument(new XmlTextReader(xmlFilePath)).CreateNavigator();

        // Stream for the transformed XML
        MemoryStream stream = new MemoryStream();

        // Load XSLT &amp; transform
        XslCompiledTransform transformer = new XslCompiledTransform();
        transformer.Load(xsltFilePath);
        transformer.Transform(nav, null, stream);

        // Write output
        using (StreamReader reader = new StreamReader(stream)) {
            stream.Position = 0;
            Response.Write(reader.ReadToEnd());
            reader.Close();
        }

    }

}
</pre>
<p>Yes, that&#8217;s it! Here below are the rest of the code used to complete a fully working demo.</p>
<p><strong>default.aspx</strong></p>
<pre class="brush: xml;">

&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;default.aspx.cs&quot; Inherits=&quot;_default&quot; %&gt;
</pre>
<p><strong>hello-world.xml</strong></p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;hello-world.xslt&quot;?&gt;
&lt;text&gt;Hello World!&lt;/text&gt;
</pre>
<p><strong>hello-world.xslt</strong></p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;iso-8859-1&quot;?&gt;
&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output indent=&quot;yes&quot; /&gt;
  &lt;xsl:output method=&quot;html&quot;/&gt;

  &lt;xsl:template match=&quot;/&quot;&gt;
&lt;xsl:text disable-output-escaping=&quot;yes&quot;&gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&amp;gt;
&lt;/xsl:text&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;&lt;xsl:value-of select=&quot;text&quot;/&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;&lt;xsl:value-of select=&quot;text&quot;/&gt;&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;
</pre>
<p><strong>Tip!</strong> To have the HTML doctype declared in your XSLT document you need to wrap it in a <em>&lt;xsl:text&gt;</em> tag with the <em>disable-output-escaping</em> attribute set to <em>yes</em>. And don&#8217;t forget to swap the leading and tailing <em>&lt;</em> and <em>&gt;</em> for <em>&amp;lt;</em> and <em>&amp;gt;</em>.</p>
<p><strong>Output</strong></p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Hello World!&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Hello World!&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://wp.garkbit.com/2009-12-14/transforming-xmlxslt-using-net-3-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript for&#8230;in</title>
		<link>http://wp.garkbit.com/2009-09-13/javascript-for-in/</link>
		<comments>http://wp.garkbit.com/2009-09-13/javascript-for-in/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 13:30:00 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[ASP.NET/C#]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mootools]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://wp.garkbit.com/?p=106</guid>
		<description><![CDATA[A couple of weeks ago a college of mine got a JavaScript error at one of our SharePoint sites. It turn out, someone at Microsoft has used a for&#8230;in loop to iterate over an Array. This works fine as long as you don&#8217;t modify the Array object. So when used with Mootools, that adds properties [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago a college of mine got a JavaScript error at one of our SharePoint sites. It turn out, someone at Microsoft has used a for&#8230;in loop to iterate over an Array. This works fine as long as you don&#8217;t modify the Array object. So when used with Mootools, that adds properties and methods to the Array object, the SharePoint script gets the hick-ups and dies.</p>
<p>The JavaScript documentation states:</p>
<blockquote><p>Iterates a specified variable over all the properties of an object, in arbitrary order. For each distinct property, the specified statement is executed.</p></blockquote>
<p>&#8230; and</p>
<blockquote><p>Although it may be tempting to use this as a way to iterate over Array elements, because the for&#8230;in statement iterates over user-defined properties in addition to the array elements, if you modify the Array object, such as adding custom properties or methods, the for&#8230;in statement will return the name of your user-defined properties in addition to the numeric indexes. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.</p></blockquote>
<p>I made a simple example to show the difference between iterating over Arrays and Objects. This is with Mootols included on the page.</p>
<p><strong>JavaScript</strong></p>
<pre class="brush: jscript;">
console.log('===== Array Example ===== ');

// Array
var a = ['alpha', 'bravo', 'charlie'];
console.log('Creating a new Array:', a);
console.log('The array length is:', a.length);

// For
console.log('--- Start For --- ');
for (var i = 0, j = a.length; i &lt; j; i++) {
    console.log(i, ':', a[i]);
}
console.log('--- End For --- ');

// For-in
console.log('--- Start For-in --- ');
for (var ii in a) {
    console.log(ii, ':', a[ii]);
}
console.log('--- End For-in --- ');
</pre>
<p><strong>Output</strong></p>
<pre class="brush: plain;">
===== Array Example =====
Creating a new Array: [&quot;alpha&quot;, &quot;bravo&quot;, &quot;charlie&quot;]
The array length is: 3
--- Start For ---
0 : alpha
1 : bravo
2 : charlie
--- End For ---
--- Start For-in ---
0 : alpha
1 : bravo
2 : charlie
$family : Object name=array
each : forEach()
clean : function()
associate : function()
link : function()
contains : function()
extend : function()
getLast : function()
getRandom : function()
include : function()
combine : function()
erase : function()
empty : function()
flatten : function()
hexToRgb : function()
rgbToHex : function()
toJSON : function()
--- End For-in ---
</pre>
<p>And here using an object.<br />
<strong></strong></p>
<p><strong>JavaScript</strong></p>
<pre class="brush: jscript;">
console.log('===== Object Example =====');

// Object
var o = { 'one': 'alpha', 'Bravo': 'bravo', 'three': 'charlie' };
console.log('Creating a new Array:', o);

// For-in
console.log('--- Start For-in --- ');
for (var item in o) {
    console.log(item, ':', o[item]);
}
console.log('--- End For-in --- ');
</pre>
<p><strong>Output</strong></p>
<pre class="brush: plain;">
===== Object Example =====
Creating a new Array: Object one=alpha Bravo=bravo three=charlie
--- Start For-in ---
one : alpha
Bravo : bravo
three : charlie
--- End For-in ---
</pre>
<p>Read more about the for&#8230;in operation at MDC Core JavaScript 1.5 Guide: <a title="MDC Core JavaScript 1.5 Guide: Object Manipulation" href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Object_Manipulation_Statements">Object Manipulation</a>.</p>
<p>Unfortunately this stupid mistake prevents us from using Mootools on this site. And we might have to reconsider the use of Mootols on more sites in the future — AND THAT SUCKS!</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.garkbit.com/2009-09-13/javascript-for-in/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
