国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現將文件轉換為XML的方法

2020-01-24 01:20:41
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現將文件轉換為XML的方法。分享給大家供大家參考,具體如下:

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;using System.Xml; namespace MyWindows{ /// <summary> /// 這個示例演示如何把Office文件編碼為xml文件以及如何把生成的xml文件轉換成Office文件 /// 把文件轉換成xml格式,然后就可以用web服務,.NET Remoting,WinSock等傳送了(其中后兩者可以不轉換也可以傳送) /// xml解決了在多層架構中數據傳輸的問題,比如說在客戶端可以用Web服務獲取服務器端的office文件,修改后再回傳給服務器 /// 只要把文件轉換成xml格式,便有好多方案可以使用了,而xml具有平臺無關性,你可以在服務端用.net用發布web服務,然后客戶端用 /// Java寫一段applit小程序來處理發送過來的文件,當然我舉的例子幾乎沒有任何顯示意義,它卻給了我們不少的啟示. /// 另外如果你的解決方案是基于多平臺的,那么他們之間的交互最好不要用遠程應用程序接口調用(RPC),應該盡量用基于文檔的交互, /// 比如說.net下的MSMQ,j2ee的JMQ. ///  /// 示例中設計到好多的類,我并沒有在所有的地方做過多注釋,有不明白的地方請參閱MSDN,這是偶第一個windows程序,有不對的地方 /// 歡迎各位指導  /// </summary> public class Form1 : System.Windows.Forms.Form {  /// <summary>  /// 聲明四個Button,一個OpenFileDialog,一個SaveFileDialog,以及兩個XmlDocument  /// </summary>  private System.Windows.Forms.Button button1;  private System.Windows.Forms.Button button2;  private System.Windows.Forms.OpenFileDialog openFileDialog1;  private System.Windows.Forms.SaveFileDialog saveFileDialog1;  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.Container components = null;  public Form1()  {   //   // Windows 窗體設計器支持所必需的   //   InitializeComponent();   //   // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼   //  }  /// <summary>  /// 清理所有正在使用的資源。  /// </summary>  protected override void Dispose( bool disposing )  {   if( disposing )   {    if(components != null)    {     components.Dispose();    }   }   base.Dispose( disposing );  }  Windows 窗體設計器生成的代碼  /// <summary>  /// 從這個入口啟動窗體   /// </summary>  static void Main()  {   Application.Run(new Form1());  }  /// <summary>  /// 把加載的Office文件轉換為xml文件  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void button1_Click(object sender, System.EventArgs e)  {    saveFileDialog1.Filter = "xml 文件|*.xml";//設置打開對話框的文件過濾條件   saveFileDialog1.Title = "保存成 xml 文件";//設置打開對話框的標題   saveFileDialog1.FileName="";   saveFileDialog1.ShowDialog();//打開對話框   if(saveFileDialog1.FileName != "")//檢測用戶是否輸入了保存文件名   {    mXmlDoc.Save(saveFileDialog1.FileName);//用私有對象mXmlDoc保存文件,mXmlDoc在前面聲明過    MessageBox.Show("保存成功");   }   }  /// <summary>  /// 把加載的xml文件轉換為Office文件  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void button2_Click(object sender, System.EventArgs e)  {   //從私有對象dox里選取me節點,這里的一些對xml對象的操作詳細說明可以參考msdn以獲取更多信息   XmlNode node=doc.DocumentElement .SelectSingleNode("me") ;   XmlElement ele=(XmlElement)node;//獲取一個xml元素   string pic=ele.GetAttribute ("aa");//獲取ele元素的aa屬性并報訊在一個臨時字符串變量pic   byte[] bytes=Convert.FromBase64String (pic);//聲明一個byte[]用來存放Base64解碼轉換過來的數據流     //從保存對話框里獲取文件保存地址   saveFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";   saveFileDialog1.Title = "保存成 office 文件";   saveFileDialog1.FileName="";   saveFileDialog1.ShowDialog();   if(saveFileDialog1.FileName != "")   {    //創建文件流并保存    FileStream outfile=new System.IO .FileStream (saveFileDialog1.FileName,System.IO.FileMode.CreateNew);    outfile.Write(bytes,0,(int)bytes.Length );    MessageBox.Show("保存成功");   }  }  /// <summary>  /// 加載窗口時的一些初始化行為  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  public void Form1_Load(object sender, System.EventArgs e)  {   MessageBox.Show("歡迎使用蛙蛙牌文檔轉換器");  }  /// <summary>  /// 卸載窗體時把臨時變量全部釋放  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  public void Form1_Closed(object sender, System.EventArgs e)  {   mXmlDoc=null;   doc=null;  }  /// <summary>  /// 加載office文件并編碼序列花為一個XmlDocument變量  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void button3_Click(object sender, System.EventArgs e)  {   string strFileName;   openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;   openFileDialog1.FilterIndex = 1;   openFileDialog1.FileName = "";   openFileDialog1.ShowDialog();   strFileName = openFileDialog1.FileName;   if(strFileName.Length != 0)   {    System.IO.FileStream inFile=new FileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.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=string.Format ("<wawa><me aa=/"{0}/"/></wawa>",mStr);    mXmlDoc.LoadXml( mStr);    MessageBox.Show("加載成功");   }   }  /// <summary>  /// 加載xml文件到私有對象dox  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void button4_Click(object sender, System.EventArgs e)  {   string strFileName;   openFileDialog1.Filter = "xml 文件|*.xml" ;   openFileDialog1.FilterIndex = 1;   openFileDialog1.FileName = "";   openFileDialog1.ShowDialog();   strFileName = openFileDialog1.FileName;   //If the user does not cancel, open the document.   if(strFileName.Length != 0)   {    doc=new XmlDocument();    doc.Load(strFileName);    MessageBox.Show("加載成功");   }  } }}

希望本文所述對大家C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 霍林郭勒市| 南木林县| 五华县| 安康市| 宜良县| 南投县| 吉安县| 岳普湖县| 辽阳县| 潢川县| 泽库县| 德江县| 八宿县| 永安市| 安国市| 民权县| 吉水县| 灵石县| 达日县| 虞城县| 天长市| 伊吾县| 全州县| 新和县| 孝义市| 安福县| 恭城| 沂源县| 于都县| 扶沟县| 崇义县| 屯门区| 永胜县| 项城市| 静安区| 海晏县| 阿克陶县| 肃北| 治多县| 色达县| 汝城县|