【问题标题】:How to create rtf using xml xslt and c#如何使用 xml xslt 和 c# 创建 rtf
【发布时间】:2011-01-29 15:56:41
【问题描述】:

我已经创建了一个 xml 文档和一个 xslt 文档,现在我想通过使用 c# 创建一个 word doc/rtf。

我在互联网上找到了以下代码,但是我找不到使用它的方法,因为我没有使用数据集。我只是想让程序读取xml和xslt将两者结合起来创建一个word文档

任何帮助将不胜感激

DataSet ds;

    XmlDataDocument xmlDoc;

    XslCompiledTransform xslTran;

    XmlElement root;

    XPathNavigator nav;

    XmlTextWriter writer;

    try

    {

        //Create the DataSet from the XML file

        ds = new DataSet();

        ds.ReadXml("Employee.xml");

        //Create the XML from the DataSet

        xmlDoc = new XmlDataDocument(ds);

        //Load the XSLT for Transformation

        xslTran = new XslCompiledTransform();

        xslTran.Load("Employee.xslt");

        //Determine the Root object in the XML

        root = xmlDoc.DocumentElement;

        //Create the XPath Navigator to navigate throuth the XML

        nav = root.CreateNavigator();

        //First delete the RTF, if already exist

        if (File.Exists("Employee.rtf"))

       {

            File.Delete("Employee.rtf");

       }

        //Create the RTF by Transforming the XML and XSLT

        writer = new XmlTextWriter("Employee.rtf", System.Text.Encoding.Default);

        xslTran.Transform(nav, writer);

        //Close the Writer after Transformation

        writer.Close();

        //Release all objects

        writer = null;

        nav = null;

        root = null;

        xmlDoc = null;

        ds = null;

        MessageBox.Show("Document created successfully.....");

    }

    catch (Exception ex)

    {

        writer = null;

        nav = null;

        root = null;

        xmlDoc = null;

        ds = null;

        MessageBox.Show(ex.StackTrace);

    }

}

【问题讨论】:

  • 题外话:最后所有的“空”赋值都是完全没有必要的。
  • 感谢您的建议。它只是我在互联网上找到的一些代码。希望它能帮助我解决我的问题

标签: c# xml ms-word xslt rtf


【解决方案1】:

阅读http://msdn.microsoft.com/en-us/library/0610k0w4.aspx,它告诉你如何使用XslCompiledTransform。如果你想从一个文件转换并输出一个文件,那么你只需要

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("stylesheet.xslt");
proc.Transform("input.xml", "output.rtf");

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多