title: XSLT invalid token results in invalid XML document
body: <p>I am using an XSLT file to transform an XML file to another XML file and then creating this XML file locally. I get this error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Token Text in state Start 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. '</p>
  </blockquote>
  
  <p>The XSLT file was debugged in visual studios and it looks like it works correctly but I don't understand this error. What does this mean and how can it be fixed?</p>
  
  <p>This is my XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;In xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="take.xsd"&gt;
    &lt;Submit ID="1234"&gt;
      &lt;Values&gt;
        &lt;Code&gt;34&lt;/Code&gt;
        &lt;Source&gt;27&lt;/Source&gt;
      &lt;/Values&gt;
      &lt;Information&gt;
        &lt;Number&gt;55&lt;/Number&gt;
        &lt;Date&gt;2018-05-20&lt;/Date&gt;
        &lt;IsFile&gt;1&lt;/IsFile&gt;
        &lt;Location&gt;&lt;/Location&gt;
        &lt;Files&gt;
          &lt;File&gt;
            &lt;Name&gt;Red.pdf&lt;/Name&gt;
            &lt;Type&gt;COLOR&lt;/Type&gt;
          &lt;/File&gt;
          &lt;File&gt;
            &lt;Name&gt;picture.pdf&lt;/Name&gt;
            &lt;Type&gt;IMAGE&lt;/Type&gt;
          &lt;/File&gt;        
        &lt;/Files&gt;
      &lt;/Information&gt;
    &lt;/Submit&gt;
  &lt;/In&gt;
  </code></pre>
  
  <p>My XSLT code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"&gt;
    &lt;xsl:output method="xml" indent="yes"/&gt;
  
    &lt;!-- identity template - copies all elements and its children and attributes --&gt;
    &lt;xsl:template match="node()|@*"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="node()|@*" /&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="/In"&gt;
      &lt;!-- Remove the 'In' element --&gt;
      &lt;xsl:apply-templates select="node()"/&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="Submit"&gt;
      &lt;!-- Create the 'Q' element and its sub-elements --&gt;
      &lt;Q xmlns:tns="Q" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.xsd" Source="{Values/Source}" Notification="true"&gt;
        &lt;xsl:copy&gt;
          &lt;xsl:copy-of select="@*"/&gt;
          &lt;xsl:apply-templates select="Values" /&gt;
          &lt;xsl:apply-templates select="Information" /&gt;
          &lt;xsl:apply-templates select="Information/Files" /&gt;
        &lt;/xsl:copy&gt;
      &lt;/Q&gt;
    &lt;/xsl:template&gt;
  
    &lt;xsl:template match="Information"&gt;
      &lt;!-- Create the 'Data' sub-element without all of its children --&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:copy-of select="Number"/&gt;
        &lt;xsl:copy-of select="Date"/&gt;
        &lt;xsl:copy-of select="IsFile"/&gt;
        &lt;xsl:copy-of select="Location"/&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And this is the C# code used to transform the file:</p>
  
  <pre><code>        XslCompiledTransform xslt = new XslCompiledTransform();
          xslt.Load(@"D:\\Main\XLSTFiles\Test.xslt");
          string xmlPath = @"D:\Documents\Test2.xml";
          using (XmlWriter w = XmlWriter.Create(@"D:\Documents\NewFile.xml"))
          {
              xslt.Transform(xmlPath, w);
          }
  </code></pre>
  
  <p>Also, is there a way to produce the new XML file with proper indentation? It seems to create each node after the last one is closed and on the custom template it just appends each item one after another.</p>