如何使用 xml xslt 和 c# 创建 rtf

发布于 2024-10-14 15:19:11 字数 1705 浏览 4 评论 0原文

我已经创建了一个 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);

    }

}

I have created an xml document and an xslt document and now i want to create a word doc/rtf using these through using c#.

I have found the following code on the internet however I can't find a way to use it because I'm not using datasets. I just want the program to read the xml and the xslt combine the two and create a word document

Any help would be really appreciated

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-10-21 15:19:11

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

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

Read http://msdn.microsoft.com/en-us/library/0610k0w4.aspx, it tells you how to use XslCompiledTransform. If you want to transform from a file and output a file then all you need is

XslCompiledTransform proc = new XslCompiledTransform();
proc.Load("stylesheet.xslt");
proc.Transform("input.xml", "output.rtf");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文