Question : C# & XSLT: How to output empty tags

I've got an xsl transform that's outputting an xml document from an xml document.
When I try to open the new xml doc in the application it's intended for I'm getting errors like
"Text is not allowed in the context of element... according to DTD/Schema".

The offending portion of the output file seems to be the hd:singleSelection element:
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
   <hd:multipleChoice name="State_1">
      <hd:options>
        <hd:option name="Alabama">
        </hd:option>
           ...
        <hd:option name="Wyoming">
        </hd:option>
      </hd:options>

      <hd:singleSelection style="dropDownList">
      </hd:singleSelection>

      <prompt>Select State</prompt>
    </hd:multipleChoice>


I presume it's looking for it in the format:
 
1:
<hd:singleSelection style="dropDownList" />


The xsl is:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
<xsl:for-each select="declarations/select">
	<xsl:element name="hd:multipleChoice">
		<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
		<xsl:element name="hd:options">
		   ...        
		</xsl:element>
		<xsl:choose>

			<xsl:when test="@display='drop-down'">
				<xsl:element name="hd:singleSelection">
					<xsl:attribute name="style">
						<xsl:text>dropDownList</xsl:text>
					</xsl:attribute>
				</xsl:element>
			</xsl:when>

			<xsl:when test="@display='radio-buttons' and @flow='tabular'">
				...
			</xsl:when>
		</xsl:choose>
	</xsl:element>
</xsl:for-each>


And the C# code is:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
FileInfo fi = new FileInfo(PathTextBox.Text);
XPathDocument xpdoc = new XPathDocument(fi.FullName);

XslTransform componentTransform = new XslTransform();
componentTransform.Load(Path.Combine(di.FullName, "ComponentFile.xslt"));
                   
string outputComponentFile = Path.Combine(fi.Directory.FullName, Path.ChangeExtension(fi.Name, "xml"));

Encoding utf8 = new UTF8Encoding(false);
XmlTextWriter componentWriter = new XmlTextWriter(outputComponentFile, utf8);
componentWriter.Formatting = Formatting.Indented;
componentWriter.WriteStartDocument();
componentTransform.Transform(xpdoc, null, componentWriter);
componentWriter.Close();


What do I change so I can get the tag to output in the format I need? I.e. the terse format of an empty tag, or at the very least so I can get it to put the tag all on one line without spaces/line breaks between the start and finish tags.
1:
<hd:singleSelection style="dropDownList"></hd:singleSelection>


Thanks.
1:
2:
<hd:singleSelection style="dropDownList">
      </hd:singleSelection>

Answer : C# & XSLT: How to output empty tags

Scratch all of the above, I just realised XslTransform is deprecated.
Replacing it exactly with XslCompiledTransform did the trick and it seems to work now and open in the target application.
Random Solutions  
 
programming4us programming4us