1

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.

4

1 に答える 1

1

XmlTextWriter に書き込むのではなく、単純にファイルに書き込むオーバーロードを使用します。

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);

.NET 2.0以降、後者は非推奨であるため、XslCompiledTransform代わりに使用しました。XslTransform

本当に使用したい場合XslTransformは、使用できる同様のTransform方法http://msdn.microsoft.com/en-us/library/x6e130yd.aspxがあります。

于 2012-11-07T12:03:08.443 に答える