<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Data Programability</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/nareshjoshi/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/nareshjoshi/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:41:30 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>How to force Non-Self Closing tags for empty nodes when using XslCompiledTransform class</title>
      <link>https://docs.microsoft.com/archive/blogs/nareshjoshi/how-to-force-non-self-closing-tags-for-empty-nodes-when-using-xslcompiledtransform-class</link>
      <pubDate>Thu, 15 Jan 2009 07:42:15 GMT</pubDate>
      <dc:creator><![CDATA[nareshjo]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/nareshjoshi/2009/01/15/how-to-force-non-self-closing-tags-for-empty-nodes-when-using-xslcompiledtransform-class/</guid>
      <description><![CDATA[In .NET 2.0 framework when using System.Xml.Xsl.XslCompiledTransform class for transforming a...]]></description>
      <content:encoded><![CDATA[<p>In .NET 2.0 framework when using System.Xml.Xsl.XslCompiledTransform class for transforming a document then we always get self closing tags for all the elements which have nothing inside their body. There is no direct way to tell it to use separate closing tags    <br />e.g.     <br />&lt;BODY&gt;     <br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;TABLE&gt;&lt;/TABLE&gt;     <br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;BR&gt;&lt;/BR&gt;     <br />&lt;/BODY&gt; </p>  <p>transforms as </p>  <p>&lt;BODY&gt;    <br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;TABLE/&gt;     <br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;BR/&gt;     <br />&lt;/BODY&gt; </p>  <p>This might cause problems when we are transforming a document for HTML processing as HTML doesn't identify self closing tags properly.    <br />So although &lt;Table/&gt; is a valid XML closed tag but HTML identifies it as starting of a table only which causes its output to be different than what we want.     <br />This thing did not happen with System.Xml.Xsl.XslTransform class in .NET 1.1 framework. It gives separate closing tags for empty elements too </p>  <p>This problem is caused because xslcompiledtransform class uses self closing tags for all the elements irrespective of whether they are empty or not. </p>  <p>The resolution of the issue involves creating a class derived from XmlTextWriter and overriding few methods in it. Here are the steps:</p>  <ul>   <li>Create a new class XmlHtmlWriter inherited from XmlTextWriter. </li>    <li>Override two functions WriteStartElement and WriteEndElement. </li>    <li>Create a list fullyClosedElements for storing names of all the tags which you want not to be self closed. </li>    <li>In WriteStartElement function record the tag which is being written and in WriteEndElement check whether we need to close this element using separate closing tag or not by checking whether it exists in fullyClosedElements list or not. </li>    <li>To use separate tag for closing call WriteFullEndElement function else call base.WriteEndElement. </li>    <li>Then use an instance of this XmlHtmlWriter class in your application for output of xsl transform. </li>    <li>Here is the code for reference. </li> </ul>  <blockquote>   <p><span style="color: blue">using</span> System;       <br /><span style="color: blue">using</span> System.Collections.Generic;       <br /><span style="color: blue">using</span> System.Text;       <br /><span style="color: blue">using</span> System.Xml; </p>    <p><span style="color: blue">namespace</span> XSLSelfClosing       <br />{       <br />&#160;&#160;&#160; <span style="color: blue">public class</span> <span style="color: #1199aa">XmlHtmlWriter</span> : <span style="color: #1199aa">XmlTextWriter</span>       <br />&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public</span> XmlHtmlWriter(System.IO.<span style="color: #1199aa">Stream</span> stream, <span style="color: #1199aa">Encoding</span> en)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; : <span style="color: blue">base</span>(stream, en)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </p>    <p><span style="color: green">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Put all the elemnts for which you want self closing tags in this list.        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Rest of the tags would be explicitely closed </span>      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fullyClosedElements.AddRange(<span style="color: blue">new string</span>[] { <span style="color: brown">&quot;br&quot;</span>, <span style="color: brown">&quot;hr&quot;</span> });       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; } </p>    <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">string</span> openingElement = <span style="color: brown">&quot;&quot;</span>;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #1199aa">List</span>&lt;<span style="color: blue">string</span>&gt; fullyClosedElements = <span style="color: blue">new</span> <span style="color: #1199aa">List</span>&lt;<span style="color: blue">string</span>&gt;(); </p>    <p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public override void</span> WriteEndElement()       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span> (fullyClosedElements.IndexOf(openingElement) &lt; 0)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WriteFullEndElement();       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">else</span>       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.WriteEndElement();       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br />      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">public override void</span> WriteStartElement(<span style="color: blue">string</span> prefix, <span style="color: blue">string</span> localName, <span style="color: blue">string</span> ns)       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">base</span>.WriteStartElement(prefix, localName, ns);       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; openingElement = localName;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }       <br />&#160;&#160;&#160; }       <br />} </p></blockquote>]]></content:encoded>
    </item>
  </channel>
</rss>