简体   繁体   中英

How to add DOCTYPE to HTML using XSLT and C#

I am using XSLT in conjunction with C# to transform my xml document into HTML. I need the DOCTYPE to be in the HTML document. But somehow I can't seem to get it to appear. Please help...

My xsl includes the following.

<?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:output method="xml" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes"/>

My C# code looks like this :

try
{
    XPathDocument myXPathDoc = new XPathDocument(myPath);

    XslTransform myXslTrans = new XslTransform();

    myXslTrans.Load(ConfigurationManager.AppSettings["XsltFilePath"] == null ? 
        "MyTransform.xsl" : 
        ConfigurationManager.AppSettings["XsltFilePath"]);

    String htmlFile = Path.Combine(myFolder, myName, "index.html");

    XmlTextWriter myWriter = new XmlTextWriter(htmlFile, null);

    myXslTrans.Transform(myXPathDoc, null, myWriter);

    myWriter.Close();
}
catch (Exception e)
{
    System.Console.WriteLine(e.Message + "\n" + e.StackTrace);
}

Any ideas what I am doing wrong? I am using .NET 4.0. Thanks in advance.

Well don't write to an XmlTextWriter, simply use an overload that writes to a file eg

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load(ConfigurationManager.AppSettings["XsltFilePath"] == null ? "MyTransform.xsl" : ConfigurationManager.AppSettings["XsltFilePath"]);

String resultFileName = Path.Combine(myFolder, myName, "index.html");

proc.Transform(myPath, resultFileName);

I used XslCompiledTransform instead of XslTransform as the latter is deprecated since .NET 2.0.

If you really want to use XslTransform then there is a similar Transform method http://msdn.microsoft.com/en-us/library/x6e130yd.aspx you can use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM