欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用C#把文件轉(zhuǎn)換為XML的代碼

 更新時(shí)間:2007年03月07日 00:00:00   作者:  
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Xml; 
namespace MyWindows
{
 /**//// <summary>
 /// 這個(gè)示例演示如何把Office文件編碼為xml文件以及如何把生成的xml文件轉(zhuǎn)換成Office文件
 /// 把文件轉(zhuǎn)換成xml格式,然后就可以用web服務(wù),.NET Remoting,WinSock等傳送了(其中后兩者可以不轉(zhuǎn)換也可以傳送)
 /// xml解決了在多層架構(gòu)中數(shù)據(jù)傳輸?shù)膯?wèn)題,比如說(shuō)在客戶端可以用Web服務(wù)獲取服務(wù)器端的office文件,修改后再回傳給服務(wù)器
 /// 只要把文件轉(zhuǎn)換成xml格式,便有好多方案可以使用了,而xml具有平臺(tái)無(wú)關(guān)性,你可以在服務(wù)端用.net用發(fā)布web服務(wù),然后客戶端用
 /// Java寫(xiě)一段applit小程序來(lái)處理發(fā)送過(guò)來(lái)的文件,當(dāng)然我舉的例子幾乎沒(méi)有任何顯示意義,它卻給了我們不少的啟示.
 /// 另外如果你的解決方案是基于多平臺(tái)的,那么他們之間的交互最好不要用遠(yuǎn)程應(yīng)用程序接口調(diào)用(RPC),應(yīng)該盡量用基于文檔的交互,
 /// 比如說(shuō).net下的MSMQ,j2ee的JMQ.
 /// 
 /// 示例中設(shè)計(jì)到好多的類,我并沒(méi)有在所有的地方做過(guò)多注釋,有不明白的地方請(qǐng)參閱MSDN,這是偶第一個(gè)windows程序,有不對(duì)的地方
 /// 歡迎各位指導(dǎo) 
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {

  /**//// <summary>
  /// 聲明四個(gè)Button,一個(gè)OpenFileDialog,一個(gè)SaveFileDialog,以及兩個(gè)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 窗體設(shè)計(jì)器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
   //
  }

  /**//// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  Windows 窗體設(shè)計(jì)器生成的代碼#region Windows 窗體設(shè)計(jì)器生成的代碼
  /**//// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(96, 32);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "生成xml";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // button2
   // 
   this.button2.Location = new System.Drawing.Point(96, 80);
   this.button2.Name = "button2";
   this.button2.TabIndex = 1;
   this.button2.Text = "生成doc";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   // 
   // button3
   // 
   this.button3.Location = new System.Drawing.Point(8, 32);
   this.button3.Name = "button3";
   this.button3.TabIndex = 2;
   this.button3.Text = "加載doc";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   // 
   // button4
   // 
   this.button4.Location = new System.Drawing.Point(8, 80);
   this.button4.Name = "button4";
   this.button4.TabIndex = 3;
   this.button4.Text = "加載xml";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(184, 141);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);
   //
   //手工注冊(cè)一下Load和Closed事件
   //
   this.Load += new System.EventHandler(this.Form1_Load);
   this.Closed += new System.EventHandler(this.Form1_Closed);

  }
  #endregion

  /**//// <summary>
  /// 從這個(gè)入口啟動(dòng)窗體 
  /// </summary>
  static void Main()
  {
   Application.Run(new Form1());
  }
  /**//// <summary>
  /// 把加載的Office文件轉(zhuǎn)換為xml文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button1_Click(object sender, System.EventArgs e)
  { 
   saveFileDialog1.Filter = "xml 文件|*.xml";//設(shè)置打開(kāi)對(duì)話框的文件過(guò)濾條件
   saveFileDialog1.Title = "保存成 xml 文件";//設(shè)置打開(kāi)對(duì)話框的標(biāo)題
   saveFileDialog1.FileName="";
   saveFileDialog1.ShowDialog();//打開(kāi)對(duì)話框

   if(saveFileDialog1.FileName != "")//檢測(cè)用戶是否輸入了保存文件名
   {
    mXmlDoc.Save(saveFileDialog1.FileName);//用私有對(duì)象mXmlDoc保存文件,mXmlDoc在前面聲明過(guò)
    MessageBox.Show("保存成功");
   } 
  }

  /**//// <summary>
  /// 把加載的xml文件轉(zhuǎn)換為Office文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button2_Click(object sender, System.EventArgs e)
  {
   //從私有對(duì)象dox里選取me節(jié)點(diǎn),這里的一些對(duì)xml對(duì)象的操作詳細(xì)說(shuō)明可以參考msdn以獲取更多信息
   XmlNode node=doc.DocumentElement .SelectSingleNode("me") ;
   XmlElement ele=(XmlElement)node;//獲取一個(gè)xml元素
   string pic=ele.GetAttribute ("aa");//獲取ele元素的aa屬性并報(bào)訊在一個(gè)臨時(shí)字符串變量pic

   byte[] bytes=Convert.FromBase64String (pic);//聲明一個(gè)byte[]用來(lái)存放Base64解碼轉(zhuǎn)換過(guò)來(lái)的數(shù)據(jù)流
  
   //從保存對(duì)話框里獲取文件保存地址
   saveFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
   saveFileDialog1.Title = "保存成 office 文件";
   saveFileDialog1.FileName="";
   saveFileDialog1.ShowDialog();

   if(saveFileDialog1.FileName != "")
   {
    //創(chuàng)建文件流并保存
    FileStream outfile=new System.IO .FileStream (saveFileDialog1.FileName,System.IO.FileMode.CreateNew);
    outfile.Write(bytes,0,(int)bytes.Length );
    MessageBox.Show("保存成功");
   }

  }

  /**//// <summary>
  /// 加載窗口時(shí)的一些初始化行為
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void Form1_Load(object sender, System.EventArgs e)
  {
   MessageBox.Show("歡迎使用蛙蛙牌文檔轉(zhuǎn)換器");
  }
  /**//// <summary>
  /// 卸載窗體時(shí)把臨時(shí)變量全部釋放
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void Form1_Closed(object sender, System.EventArgs e)
  {
   mXmlDoc=null;
   doc=null;
  }
  /**//// <summary>
  /// 加載office文件并編碼序列花為一個(gè)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文件到私有對(duì)象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("加載成功");
   }

  }

 }
}

相關(guān)文章

  • C#操作注冊(cè)表之RegistryKey類

    C#操作注冊(cè)表之RegistryKey類

    這篇文章介紹了C#操作注冊(cè)表之RegistryKey類,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#清理非托管對(duì)象實(shí)例分析

    C#清理非托管對(duì)象實(shí)例分析

    這篇文章主要介紹了C#清理非托管對(duì)象的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#清理非托管對(duì)象釋放資源的相關(guān)原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-02-02
  • C# List介紹及具體用法

    C# List介紹及具體用法

    這篇文章主要介紹了C# List介紹及具體用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • C#中38個(gè)常用運(yùn)算符的優(yōu)先級(jí)的劃分和理解

    C#中38個(gè)常用運(yùn)算符的優(yōu)先級(jí)的劃分和理解

    這只我自己在學(xué)C#中的一些總結(jié),其中對(duì)于各級(jí)的劃分方式、各操作符的優(yōu)先級(jí)的理解并不見(jiàn)得正確,只是自己的看法,拿出來(lái)與大家分享
    2012-08-08
  • Unity實(shí)現(xiàn)物體運(yùn)動(dòng)軌跡的繪制

    Unity實(shí)現(xiàn)物體運(yùn)動(dòng)軌跡的繪制

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體運(yùn)動(dòng)軌跡的繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C#中使用反射遍歷一個(gè)對(duì)象屬性及值的小技巧

    C#中使用反射遍歷一個(gè)對(duì)象屬性及值的小技巧

    這篇文章主要介紹了C#中使用反射遍歷一個(gè)對(duì)象屬性及值的小技巧,這在很時(shí)候應(yīng)該都非常有用,本文直接給出實(shí)例代碼,需要的朋友可以參考下
    2015-07-07
  • C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號(hào)的方法

    C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號(hào)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號(hào)的方法,比較實(shí)用的功能,需要的朋友可以參考下
    2014-07-07
  • .NET C#利用ZXing生成、識(shí)別二維碼/條形碼

    .NET C#利用ZXing生成、識(shí)別二維碼/條形碼

    ZXing是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。這篇文章主要給大家介紹了.NET C#利用ZXing生成、識(shí)別二維碼/條形碼的方法,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒。
    2016-12-12
  • C#判斷語(yǔ)句的表達(dá)式樹(shù)實(shí)現(xiàn)

    C#判斷語(yǔ)句的表達(dá)式樹(shù)實(shí)現(xiàn)

    這篇文章介紹了C#判斷語(yǔ)句的表達(dá)式樹(shù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • C#使用Win2D在UWP程序中實(shí)現(xiàn)2D繪圖

    C#使用Win2D在UWP程序中實(shí)現(xiàn)2D繪圖

    這篇文章介紹了C#使用Win2D在UWP程序中實(shí)現(xiàn)2D繪圖的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論