C#完成將文件轉換為XML的辦法。本站提示廣大學習愛好者:(C#完成將文件轉換為XML的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成將文件轉換為XML的辦法正文
本文實例講述了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#法式設計有所贊助。