<?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>Tim Van Wassenhove &#187; XML</title>
	<atom:link href="http://www.timvw.be/category/information-technology/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:07:31 +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>Writing Xml without the XmlDeclaration</title>
		<link>http://www.timvw.be/writing-xml-without-the-xmldeclaration/</link>
		<comments>http://www.timvw.be/writing-xml-without-the-xmldeclaration/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 18:16:02 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/writing-xml-without-the-xmldeclaration/</guid>
		<description><![CDATA[Consider the following xml file: &#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; ?&#62; &#60;!-- some comment --&#62; &#60;root&#62; &#60;/root&#62; &#60;!-- another comment --&#62; If you look at the Well-Formed XML Documents section in the XML specification you see that a well-formed document is defined as: [1] document ::= prolog element Misc* Since there is only 1 root element (ever), [...]]]></description>
			<content:encoded><![CDATA[<p>Consider the following xml file:</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;!-- some comment --&gt;
&lt;root&gt;
&lt;/root&gt;
&lt;!-- another comment --&gt;
</pre>
<p>If you look at the <a href="http://www.w3.org/TR/REC-xml/#sec-well-formed">Well-Formed XML Documents</a> section in the <a href="http://www.w3.org/TR/REC-xml/">XML specification</a>  you see that a well-formed document is defined as:</p>
<blockquote>
[1] document ::= <a href="http://www.w3.org/TR/REC-xml/#NT-prolog">prolog</a> <a href="http://www.w3.org/TR/REC-xml/#NT-element">element</a> <a href="http://www.w3.org/TR/REC-xml/#NT-Misc">Misc*</a>
</blockquote>

<p>Since there is only 1 root element (ever), i assumed that if you <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.load.aspx">Load</a> this file with <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmldocument.aspx">XmlDocument</a> their would be only one <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.aspx">XmlNode</a> in the document <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.childnodes.aspx">ChildNodes</a>. In reality there ChildNodes.Count returns 4.</p>

<p>The simplest way to write this XmlDocument without the declaration would be as following:</p>

<pre class="brush: csharp;">XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.OmitXmlDeclaration = true;
xmlWriterSettings.Encoding = Encoding.UTF8;
using (XmlWriter writer = XmlWriter.Create(@&quot;result.xml&quot;, xmlWriterSettings))
{
 xmlDoc.WriteTo(writer);
}
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/writing-xml-without-the-xmldeclaration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating UTF-8 with System.Xml.XmlWriter</title>
		<link>http://www.timvw.be/generating-utf-8-with-systemxmlxmlwriter/</link>
		<comments>http://www.timvw.be/generating-utf-8-with-systemxmlxmlwriter/#comments</comments>
		<pubDate>Mon, 08 Jan 2007 21:59:44 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/generating-utf-8-with-systemxmlxmlwriter/</guid>
		<description><![CDATA[Today i decided to experiment with XmlWriter. The first i wanted to do was set the Encoding to UTF-8.: StringBuilder stringBuilder = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create(stringBuilder); xmlWriter.Settings.Encoding = Encoding.UTF8; When i ran this code i recieved the following exception: XmlException was unhandled: The &#8216;XmlWriterSettings.Encoding&#8217; property is read only and cannot be set. The [...]]]></description>
			<content:encoded><![CDATA[<p>Today i decided to experiment with <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmlwriter.aspx">XmlWriter</a>. The first i wanted to do was set the Encoding to UTF-8.:</p>

<pre class="brush: csharp;">StringBuilder stringBuilder = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
xmlWriter.Settings.Encoding = Encoding.UTF8;</pre>

<p>When i ran this code i recieved the following exception: XmlException was unhandled: The &#8216;XmlWriterSettings.Encoding&#8217; property is read only and cannot be set. The documentation for the <a href="http://msdn2.microsoft.com/en-us/library/system.xml.xmlwriter.settings.aspx">Settings</a> property clearly says:</p>

<blockquote>
<div>
The XmlWriterSettings object returned by the Settings property cannot be modified. Any attempt to change individual settings results in an exception being thrown.
</div>
</blockquote>

<p>So i wrote the following:</p>

<pre class="brush: csharp;">StringBuilder stringBuilder = new StringBuilder();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;

XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(&quot;root&quot;, &quot;http://www.timvw.be/ns&quot;);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();

string xmlString = stringBuilder.ToString();</pre>

<p>As you can see: <b>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-16&#8243;?&gt;&lt;root xmlns=&#8221;http://www.timvw.be/ns&#8221; /&gt;</b> is still not what i want. Apparently is the Encoding property ignored if the XmlWriter is not using a Stream. So here is my next attempt:</p>

<pre class="brush: csharp;">MemoryStream memoryStream = new MemoryStream();
// initialize xmlWriterSettings as above...

XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
// call the same operations on the xmlWriter as above...

string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());</pre>
<p>Ok, i&#8217;m getting close: <b>?&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;&lt;root xmlns=&#8221;http://www.timvw.be/ns&#8221; /&gt;</b>. Luckily enough i knew that the ? (byte with value 239) at the beginning is the <a href="http://en.wikipedia.org/wiki/Byte_Order_Mark">BOM</a>. In order to get rid of that byte i had to create my own instance of <a href="http://msdn2.microsoft.com/en-us/library/system.text.utf8encoding.aspx">UTF8Encoding</a>. Finally, i can present some working code:</p>
<pre class="brush: csharp;">MemoryStream memoryStream = new MemoryStream();
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
xmlWriterSettings.ConformanceLevel = ConformanceLevel.Document;
xmlWriterSettings.Indent = true;

XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(&quot;root&quot;, &quot;http://www.timvw.be/ns&quot;);
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();

string xmlString = Encoding.UTF8.GetString(memoryStream.ToArray());</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/generating-utf-8-with-systemxmlxmlwriter/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Accept posted XML data</title>
		<link>http://www.timvw.be/accept-posted-xml-data/</link>
		<comments>http://www.timvw.be/accept-posted-xml-data/#comments</comments>
		<pubDate>Wed, 18 Jan 2006 00:57:07 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/accept-posted-xml-data/</guid>
		<description><![CDATA[I remember that i&#8217;ve spent a lot of time finding something that allowed me to accept the posted XML data. The solution was very simple: $data = file_get_contents('php://input');]]></description>
			<content:encoded><![CDATA[<p>I remember that i&#8217;ve spent a lot of time finding something that allowed me to accept the posted XML data. The solution was very simple:</p>
<pre class="brush: php;">
$data = file_get_contents('php://input');
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/accept-posted-xml-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>value-of in an attribute</title>
		<link>http://www.timvw.be/value-of-in-an-attribute/</link>
		<comments>http://www.timvw.be/value-of-in-an-attribute/#comments</comments>
		<pubDate>Mon, 25 Jul 2005 00:02:38 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/value-of-in-an-attribute/</guid>
		<description><![CDATA[There were days that i didn&#8217;t like XSL because it seemed to be impossible to insert a value inside an attribute. For example: &#60;form action=&#34;&#60;xsl:value-of select=&#34;/page/action&#34;/&#62;&#34; method=&#34;post&#34;&#62; Today i&#8217;ve seen that other people also struggle with this issue. So here is the solution: &#60;form action=&#34;{/page/action}&#34; method=&#34;post&#34;&#62; &#60;/form&#62;]]></description>
			<content:encoded><![CDATA[<p>There were days that i didn&#8217;t like XSL because it seemed to be impossible to insert a value inside an attribute. For example:</p>

<pre class="brush: xml;">
&lt;form action=&quot;&lt;xsl:value-of select=&quot;/page/action&quot;/&gt;&quot; method=&quot;post&quot;&gt;
</pre>

<p>Today i&#8217;ve seen that other people also struggle with this issue. So here is the solution:</p>

<pre class="brush: xml;">
&lt;form action=&quot;{/page/action}&quot; method=&quot;post&quot;&gt;
&lt;/form&gt;</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/value-of-in-an-attribute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing with XML and XSL</title>
		<link>http://www.timvw.be/playing-with-xml-and-xsl/</link>
		<comments>http://www.timvw.be/playing-with-xml-and-xsl/#comments</comments>
		<pubDate>Sat, 22 Jan 2005 23:16:39 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/playing-with-xml-and-xsl/</guid>
		<description><![CDATA[// add stuff to an xml document in php4 $doc = domxml_open_mem($xml); $root = $doc-&#62;document_element(); $inner = $doc-&#62;create_element('inner'); $root = $root-&#62;append_child($inner); // add stuff to an xml document in php5 $doc = new DomDocument('1.0', 'UTF-8'); $doc-&#62;loadXML($xml); $root = $doc-&#62;getelementsByTagName('resultset')-&#62;item(0); $inner = $doc-&#62;createElement('inner'); $root = $root-&#62;appendChild($inner); XHTML does not allow to have an empty list, &#60;ul&#62;&#60;/ul&#62;. [...]]]></description>
			<content:encoded><![CDATA[<pre class="brush: php;">
// add stuff to an xml document in php4
$doc = domxml_open_mem($xml);
$root = $doc-&gt;document_element();
$inner = $doc-&gt;create_element('inner');
$root = $root-&gt;append_child($inner);

// add stuff to an xml document in php5
$doc = new DomDocument('1.0', 'UTF-8');
$doc-&gt;loadXML($xml);
$root = $doc-&gt;getelementsByTagName('resultset')-&gt;item(0);
$inner = $doc-&gt;createElement('inner');
$root = $root-&gt;appendChild($inner);
</pre>
<p><a href="http://www.w3.org/TR/xhtml1/">XHTML</a> does not allow to have an empty list, &lt;ul&gt;&lt;/ul&gt;. Therefore we need to test first if there are any nodes we want to put in that list. The code to do this looks like:</p>
<pre class="brush: xml;">
&lt;xsl:for-each select=&quot;//resultset/entity&quot;&gt;
  &lt;div class=&quot;mainitem&quot;&gt;
  &lt;div class=&quot;maintitle&quot;&gt;&lt;xsl:value-of select=&quot;title&quot;/&gt;&lt;/div&gt;
  &lt;div class=&quot;maincontent&quot;&gt;
    &lt;xsl:if test=&quot;count(items/item) &amp;gt; 0&quot;&gt;
      &lt;ul&gt;
        &lt;xsl:for-each select=&quot;items/item&quot;&gt;
        &lt;li&gt;&lt;a href=&quot;{link}&quot;&gt;&lt;xsl:value-of select=&quot;title&quot;/&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;/xsl:for-each&gt;
      &lt;/ul&gt;
    &lt;/xsl:if&gt;
  &lt;/div&gt;
  &lt;/div&gt;
&lt;/xsl:for-each&gt;
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/playing-with-xml-and-xsl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XSLT annoyances</title>
		<link>http://www.timvw.be/xslt-annoyances/</link>
		<comments>http://www.timvw.be/xslt-annoyances/#comments</comments>
		<pubDate>Wed, 19 Jan 2005 23:15:04 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/xslt-annoyances/</guid>
		<description><![CDATA[Today i&#8217;ve finally made the switch. My code generates XML and then i translate it to XHTML with XSLT. However, if i write &#60;textarea name=&#34;foo&#34;&#62;&#60;/textarea&#62; it will be translated to: &#60;textarea name=&#34;foo&#34;/&#62;. A not so good workaround is to write: (Notice the space in the xsl:text) &#60;textarea name=&#34;foo&#34;&#62;&#60;xsl:text&#62; &#60;/xsl:text&#62;&#60;/textarea&#62; UPDATE on 2005-01-20 05:42 The solution [...]]]></description>
			<content:encoded><![CDATA[<p>Today i&#8217;ve finally made the switch. My code generates <a href="http://www.w3.org/XML/">XML</a> and then i translate it to <a href="http://www.w3.org/TR/xhtml1/">XHTML</a> with <a href="http://www.w3.org/TR/xslt">XSLT</a>. However, if i write </p>
<pre class="brush: xml;">
&lt;textarea name=&quot;foo&quot;&gt;&lt;/textarea&gt;
</pre>
<p>it will be translated to:</p>
<pre class="brush: xml;">
&lt;textarea name=&quot;foo&quot;/&gt;. 
</pre>
<p>A not so good workaround is to write: (Notice the space in the xsl:text)</p>
<pre class="brush: xml;">
&lt;textarea name=&quot;foo&quot;&gt;&lt;xsl:text&gt; &lt;/xsl:text&gt;&lt;/textarea&gt;
</pre> 

<p>UPDATE on 2005-01-20 05:42
The solution is to use html as output method instead of xml.</p> 
<pre class="brush: xml;">
&lt;xsl:output method='html'/&gt;
</pre>

<p>UPDATE on 2005-01-21 02:15
You may also want to make sure  HTML tags do not get transformed:</p>
 <pre class="brush: xml;">
&lt;xsl:value-of select=&quot;attribute[@name='content']&quot; disable-output-escaping=&quot;yes&quot;/&gt;
</pre>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/xslt-annoyances/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing XML</title>
		<link>http://www.timvw.be/parsing-xml/</link>
		<comments>http://www.timvw.be/parsing-xml/#comments</comments>
		<pubDate>Mon, 05 Apr 2004 22:42:36 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.timvw.be/parsing-xml/</guid>
		<description><![CDATA[Today i wrote a little program, VolumeMeter which is usefull for Scarlet customers. It queries mijn.scarlet.be and returns how many megabytes they have used this month.]]></description>
			<content:encoded><![CDATA[<p>Today i wrote a little program, <a href="http://www.timvw.be/wp-content/code/csharp/VolumeMeter.txt">VolumeMeter</a> which is usefull for <a href="http://www.scarlet.be">Scarlet</a> customers. It queries <a href="http://mijn.scarlet.be">mijn.scarlet.be</a> and returns how many megabytes they have used this month.</p>]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/parsing-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
