I am seeing some weird behavior (or I am missing something) with the following nUnit test:
[Test]
public void Test() {
const string works = @"
<doc>
<simple>simple</simple>
<ItemDef>
<Description>
</Description>
</ItemDef>
</doc>
";
const string doesntWork = @"
<doc>
<simple>simple</simple>
<ItemDef>
<Description>
<TranslatedText>3</TranslatedText>
</Description>
</ItemDef>
</doc>
";
string xsl = @"<?xml version='1.0' encoding='iso-8859-1'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output indent='yes'/>
<xsl:template match='simple'>
<node>
</node>
</xsl:template>
</xsl:stylesheet>
";
TransformXml2(works, xsl);
TransformXml2(doesntWork, xsl);
}
protected static void TransformXml2(string xml, string xsl) {
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(XmlReader.Create(new StringReader(xsl)));
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
XmlReader input = new XmlNodeReader(document.DocumentElement);
transform.Transform(input, new XsltArgumentList(), XmlTextWriter.Create(new StringBuilder()));
}
When I run this test, the second TransformXml2 call fails with the following error:
Token Text in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.
The only difference between the two pieces of XML is the following node:
<TranslatedText>3</TranslatedText>
Anyone have any idea what is going on?
Just so you know, I am aware the TransformXml2 call does nothing useful. I just wanted to provide some executable code that demonstrates the error.
The reason is that the second input contains text after the
<simple>simple</simple>element that you’re not handling. The default (built-in) template for processing text nodes is outputting that text after the<node></node>, which results in a document that’s not well-formed:That’s what the processor is warning you about.