简体   繁体   English

如何使用XSLT和C#将DOCTYPE添加到HTML

[英]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. 我将XSLT与C#结合使用,将xml文档转换为HTML。 I need the DOCTYPE to be in the HTML document. 我需要DOCTYPE在HTML文档中。 But somehow I can't seem to get it to appear. 但是我似乎无法使它出现。 Please help... 请帮忙...

My xsl includes the following. 我的xsl包括以下内容。

<?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 : 我的C#代码如下所示:

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. 我正在使用.NET 4.0。 Thanks in advance. 提前致谢。

Well don't write to an XmlTextWriter, simply use an overload that writes to a file eg 好吧,不要写入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);

I used XslCompiledTransform instead of XslTransform as the latter is deprecated since .NET 2.0. 我使用XslCompiledTransform代替XslTransform ,因为从.NET 2.0开始不推荐使用XslTransform

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. 如果您确实要使用XslTransform则可以使用类似的Transform方法http://msdn.microsoft.com/en-us/library/x6e130yd.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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