如何使用XSLT两次转换XML?

时间:2017-06-28 09:32:59

标签: c# html xml xslt

我必须使用不同的.xslt文件转换.xml两次,我知道如何转换它一次,但是没有想法,如何重复它,因为当我的输出类似于html字符串并且我改变了我的代码,通过使用XmlWriter而不是StringWriter(注释),但它生成一个空的Xml,因此无法再次传输。

public static HtmlString RenderXml(this HtmlHelper helper, string xml, string transformXsltPath, string xsltPath)
    {
        xml = System.IO.File.ReadAllText(("C:/Users/Student/Documents/Visual Studio 2010/Projects/MvcApplication2/MvcApplication2/schemat.xsd"));

        XsltArgumentList args = new XsltArgumentList();
        XslCompiledTransform t = new XslCompiledTransform();
        t.Load(transformXsltPath);

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.DtdProcessing = DtdProcessing.Parse;;
        using (XmlReader reader = XmlReader.Create(new StringReader(xml), settings))
        {
            //StringWriter writer = new StringWriter();
            XmlWriter writer = XmlWriter.Create(new StringWriter());
            t.Transform(reader, args, writer);
            XslCompiledTransform t2 = new XslCompiledTransform();
            t2.Load(xsltPath);
            XmlReader reader2 = XmlReader.Create(new StringReader(writer.ToString()), settings);

            StringWriter writer2 = new StringWriter();
            t2.Transform(reader2, args, writer2);

            HtmlString htmlString = new HtmlString(writer2.ToString());

            return htmlString;
        }
    }

0 个答案:

没有答案