【发布时间】: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);
}
}
【问题讨论】:
-
题外话:最后所有的“空”赋值都是完全没有必要的。
-
感谢您的建议。它只是我在互联网上找到的一些代码。希望它能帮助我解决我的问题