2014-01-09 36 views
1

XML文件中有如下內容:如何把XML文件轉換爲Doc文件?

<?xml version="1.0"?> 
    <note> 
     <to>Tove</to> 
     <from>Jani</from> 
     <heading>Reminder</heading> 
     <body>Don't forget me this weekend! </body> 
    </note> 

我必須將它轉換爲doc文件。我正在使用Spire.Doc組件進行轉換。但它給下面的錯誤:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "

我使用下面的代碼片段:

Document doc = new Document(); 
doc.LoadFromFile(textBox1.Text, FileFormat.Xml); 
doc.SaveToFile(DocfileLocation, FileFormat.Doc); 

是否有任何其他的方法來轉換呢?

+0

歡迎來到堆棧溢出,我編輯了你的問題,這裏是爲什麼:http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their -titles –

回答

0

它應該按照您提供的方式工作。難道你不是逃避「\」字符或類似的?作爲測試嘗試將XML文件sample.xml中保存在同一文件夾中可執行文件將被編譯並運行,而不是這個片段:

Document doc = new Document(); 
doc.LoadFromFile("sample.xml", FileFormat.Xml); 
doc.SaveToFile("test.doc", FileFormat.Doc); 

這應該給你的內容的新的.doc文件其中的.xml文件

+0

應用此代碼段後,我收到此錯誤:「文檔中沒有任何部分」。它正在創建的文檔不是顯示xml的內容,而是顯示utf-8格式的編碼。但是我想顯示xml節點的內容。 – user

+0

您用來測試的sample.xml文件是否包含實際的xml數據? – Bensonius