2011-01-29 45 views
1

我已經創建了一個xml文檔和一個xslt文檔,現在我想通過使用c#創建一個doc doc/rtf文檔。如何使用xml xslt和c創建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); 

    } 

} 
+1

題外話:最後所有的「空」分配是完全沒有必要的。 – 2011-01-29 15:59:25

+0

感謝您的意見。它只是我在互聯網上找到的一些代碼。希望能幫助我解決我的問題 – lilly1 2011-01-29 16:05:05

回答

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