




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、用C#把文件轉(zhuǎn)換為XMLusing System;using System .Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;using System.XML;namespace MyWindowsIIIIII這個示例演示如何把Office文件編碼為XML文件以及如 何把生成的xml文件轉(zhuǎn)換成Office文件III把文件轉(zhuǎn)換成xml格式,然后就可以用Web服務,.net Remoting,WinSock等傳送了 (其中后兩者可以不轉(zhuǎn)換也
2、可以傳送)III xml解決了在多層架構中數(shù)據(jù)傳輸?shù)膯栴}上匕如說在客戶端 可以用Web服務獲取服務器端的office文件,修改后再回傳給服務器III只要把文件轉(zhuǎn)換成xml格式便有好多方案可以使用了,而 xml具有平臺無關性,你可以在服務端用.net用發(fā)布web服務撚后客 戶端用Ill Java寫一段applit小程序來處理發(fā)送過來的文件,當然我舉 的例子幾乎沒有任何顯示意義,它卻給了我們不少的啟示III另外如果你的解決方案是基于多平臺的,那么他們之間的 交互不要用遠程應用程序接口調(diào)用(RPC),應該盡量用基于文檔的交 互,III 比如說.net 下的 MSMQJ2EE 的 JMQ.IllIII
3、示例中設計到好多的類,我并沒有在所有的地方做過多注 釋,有不明白的地方請參閱MSDN,這是偶第一個windows程序,有不 對的地方III歡迎各位指導IIIpublic class Forml: System.Windows.Forms.FormIIIIII 聲明四個 Button,一個 OpenFileDialog,個 SaveFileDialog,以及兩個 XmlDocumentIIIprivate System.Windows.Forms.Button buttonl;private System.Windows.Forms.Button button2; private System.
4、Windows.Forms.OpenFileDialogope nFileDialogl;private System.Windows.Forms.SaveFileDialogsaveFileDialogl;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button4;private System.Xml.XmlDocument mXmlDoc;private System.Xml.XmlDocument doc;private System.ComponentModel.Cont
5、ainer components = n ull;public Forml()/ Windows窗體設計器支持所必需的/In itializeComp onent();/ TODO:在InitializeComponent調(diào)用后添加任彳可構 造函數(shù)代碼/IIIIII清理所有正在使用的資源。IIIprotected override void Dispose( bool disposing )if( disposing )if(components != null)compone nts.Dispose();base.Dispose( disposing );#region Windows窗體設
6、計器生成的代碼IIIIII設計器支持所需的方法不要使用代碼編輯器修改III此方法的內(nèi)容。IIIprivate void InitializeComponent()this.buttonl = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.ope nFileDialogl = newSystem.Windows.Forms.OpenFileDialogO;this.saveFileDialogl = newSystem.Windows.Forms.SaveFileDi
7、alogO;this.button3 = new System.Windows.Forms.Button();this.button4 二 new System.Windows.Forms.Button(); this.SuspendLayout();/ buttonl/this.butt onLL ocation = newSystem.Drawing.Point(96z 32);this.butt on l.Name = butt onl;this.butt on l.Tablndex = 0;this.buttonl.Text =生成 xml;this.butt on l.Click +
8、= newSystem.EventHandler(this.buttonl_Click);/ button2/this.button2丄ocation 二 newSystem.Drawing.Point(96, 80);this.button2.Name = nbutton2;this.butt on 2.TabIndex = 1;this.button2.Text =生成 doc;this.butt on 2.Click += newSystem.EventHa ndler(this.butt on 2_Click);/ button3/this.button3丄ocation 二 new
9、System.Drawing.Point(8,32);this.butt on 3.Name = nbutt on3;this.button3.TabIndex = 2;this.button3.Text =加載 doc;this.button3.Click += newSystem.EventHa ndler(this.butt on 3_Click);/ button4/this.button4丄ocation = new System.Drawing.Point(8,80);this.butto n4.Name = butt on4;this.butt on 4.TabIndex = 3
10、;this.button4.Text =加載 xml;this.butt on 4.Click += newSystem.EventHandler(this.button4_Click);/ Forml/this.AutoScaleBaseSize = newSy st e m .Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(184# 141);this.Controls.Add(this.butto n4);this.C on trols.Add(this.butt on3);this.C on trols.Add
11、(this.butt on2);this.C on trols.Add(this.butt onl);this.Name = Forml;this.Text = Forml;this.ResumeLayout(false);/手工注冊一下Load和Closed事件/this 丄 oad += newSystem.EventHa ndler(this.Forml_Load);this.Closed += newSystem.EventHa ndler(this.Forml_Closed);#endregi onIIIIII從這個入口啟動窗體IIIstatic void Main()Applica
12、tion.Run(new Forml();IIIIII把加載的Office文件轉(zhuǎn)換為xml文件IIIIIIIIIprivate void buttonl_Click(object sender;Sy stem. Eve ntArgs e)saveFileDialogl.Filter = xml 文件|*xml;設置打開對話框的文件過濾條件saveFileDialogl.Title = ”保存成 xml 文件:設置打開對話框的標題saveFileDialogl.FileName=nsaveFileDialogl.ShowDialog(); 打開對話框if(saveFileDialogl.File
13、Name != ”“)/檢測用戶是否輸入 了保存文件名mXmlDoc.Save(saveFileDialogl.FileName);/ 用私有對象mXmlDoc保存文件”XmlDoc在前面聲明過MessageBox.Show(n 保存成功”);IIIIII把加載的xml文件轉(zhuǎn)換為Office文件IIIIIIIIIprivate void butt on 2_Click(object sender;Sy stem. Eve ntArgs e)從私有對象dox里選取me節(jié)點,這里的一些對xml對象的操作詳細說明可以參考msdn以獲取更多信息XmlNodenode=doc.DocumentEleme
14、nt .SelectSingleNode(me);XmlElement ele=(XmlElement)node;/獲取一個 xml元素string pic=ele.GetAttribute (aa);獲取 ele 元素的 aa 屬性并報訊在一個臨時字符串變量picbyte bytes=Convert.FromBase64String (pic);/聲明個byte用來存放Base64解碼轉(zhuǎn)換過來的數(shù)據(jù)流從保存對話框里獲取文件保存地址saveFileDialogl.Filter = Office Documents.doc,*15, *.ppt)|*.doc;*.xls;*.ppt;saveFi
15、leDialogl.Title =保存成 office 文件; saveFileDialogl.FileName=nsaveFileDialogl.ShowDialog();if(saveFileDialogl.FileName !=)創(chuàng)建文件流并保存FileStream outfile=new Sy stem.10 .FileStream(saveFileDialogl.FileNamezSystem.IO.FileMode.CreateNew);outfile.Write(bytes,0,(int)bytes.Length );MessageBox.Show(保存成功”);IIIIII加載
16、窗口時的一些初始化行為IllIIIIIIpublic void Forml_Load(object sender;Sy stem. Eve ntArgs e)MessageBox.Show(n歡迎使用蚌蚌牌文檔轉(zhuǎn)換器“);IIIIII卸載窗體時把臨時變量全部釋放IIIIIIIIIpublic void Forml_Closed(object sender;Sy stem. Eve ntArgs e)mXmlDoc=null;doc=null;IIIIII加載office文件并編碼序列花為一個XmlDocument變IllIIIIIIprivate void butt on 3_Click(ob
17、ject sender;System.EventArgs e)string strFileName;ope nF ileDialogl.Filter = Office Documents(*.docz*.xls# *.ppt)|*.doc;*.xls;*.pptn;openFileDialogl.Filterindex = 1;ope nFileDialogl.FileName =ope nFileDialogl.ShowDialog();strFileName = ope nF ileDialogl.FileName;if(strFileName.Length != 0)Sy ste m .
18、IO. F i I eSt rea m inF ile=newFileStream(strFileName/System.IO.FileMode.Open/System.IO.Fil eAccess.Read);byte binaryData二new byte inFile.Length;inFile.Read(binaryData, 0#(int)inFile.Length);string mStr=Convert.ToBase64String(binaryData); string hh = mStr;mXmlDoc二new System.Xml.XmlDocument();mStr=st
19、ring.Format (”;mStr);mXmlDoc丄oadXml( mStr);MessageBox.Show(n 加載成功”);IIIIII加載xml文件到私有對象doxIIIIIIIIIprivate void butt on 4_Click(object sender;System.EventArgs e)string strFileName;openFileDialogl.Filter = nxml 文件廣.xml;ope nF ileDialogl.Filterl ndex = 1;ope nFileDialogl.FileName =ope nFileDialogl.ShowDialogO;strFileName = ope nFileDialogl.FileName;/If the user does not cancelz open the documentif(strFileName.Length != 0)doc二new XmlDocument(); doc.Load(strFileName);MessageBox.Show(n 加載成功”
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 西安交通大學《中國西部文學研究》2023-2024學年第二學期期末試卷
- 廈門理工學院《教育經(jīng)典名著與影片賞析》2023-2024學年第二學期期末試卷
- 《乘法的初步認識》 (教學設計)-2024-2025學年二年級上冊數(shù)學人教版
- 大連裝備制造職業(yè)技術學院《數(shù)據(jù)庫原理與應用》2023-2024學年第二學期期末試卷
- 沈陽工程學院《人體解剖生理學實驗》2023-2024學年第二學期期末試卷
- 展覽布置合同范本
- 太陽能空調(diào)系統(tǒng)項目效益評估報告
- 河南工業(yè)貿(mào)易職業(yè)學院《投資項目評估》2023-2024學年第二學期期末試卷
- 桂林生命與健康職業(yè)技術學院《機械原理》2023-2024學年第二學期期末試卷
- 上海濟光職業(yè)技術學院《小學音樂課程與教學》2023-2024學年第二學期期末試卷
- 《中小學科學教育工作指南》解讀與培訓
- 學校食堂“三同三公開”制度實施方案
- 2025年湖南高速鐵路職業(yè)技術學院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 殯儀館管理制度
- 2025年醫(yī)院財務工作計劃(2篇)
- DB32T 4969-2024大型醫(yī)用設備使用監(jiān)督管理平臺基礎數(shù)據(jù)采集規(guī)范
- 部編版教科版三年級科學下冊全冊教案【統(tǒng)編教材】
- (2024)甘肅省公務員考試《行測》真題及答案解析
- 小紅書種草營銷師(初級)認證考試真題試題庫(含答案)
- 藥品經(jīng)營使用和質(zhì)量監(jiān)督管理辦法2024年宣貫培訓課件
- 康復治療技術(師)基礎知識考試考點內(nèi)容總結
評論
0/150
提交評論