开发者

How to create rtf using xml xslt and c#

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;

       开发者_如何学C ds = null;

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

    }

    catch (Exception ex)

    {

        writer = null;

        nav = null;

        root = null;

        xmlDoc = null;

        ds = null;

        MessageBox.Show(ex.StackTrace);

    }

}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜