views:

65

answers:

2

I am doing an xslt transform inside my c# program. When I run the xslt on its own it outputs just fine, but when I run it from within my c# program it always leaves off the:

<?xml version="1.0" encoding="UTF-8"?>

At the top of the resulting xml document. My XSLT file looks like:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:hd="http://www.hotdocs.com/schemas/component_library/2009"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"&gt;

  <xsl:output method="xml" omit-xml-declaration="no" version="1.0" encoding="UTF-8"/>

  <xsl:template match="/xsd:schema">
     <hd:componentLibrary xmlns:hd="something" version="10">
     </hd:componentLibrary>
  </xsl:template>
  <xsl:template match="text()" />
</xsl:stylesheet>

I am running the xslt in my c# program like this:

XPathDocument myXPathDoc = new XPathDocument(PathToXMLDocument);
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(PathToXSLTDocument);
XmlTextWriter myWriter = new XmlTextWriter(PathToOutputLocation, null);
myXslTrans.Transform(myXPathDoc,null,myWriter);
myWriter.Close();

I have tried the xslt document without the xsl:output line, but that does not seem to help.

How can i get the ?xml tag at the top of my outputted xml file?

Thanks

+1  A: 

XmlTextWriter is a bit outdated. I recommend you switch to XmlWriter.Create.

Then you can specify OmitXmlDeclaration = false in the XmlWriterSettings.

dtb
A: 

If you use XmlWriter.Create() then you can pass an XmlWriterSettings instance as a parameter. The OmitXmlDeclaration member in the settings class controls whether or not the tag is included.

matiash