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具有平台⽆关性,你可以在服务端⽤⽤发布web服务,然后客户端⽤ /// Java写⼀段applit⼩程序来处理发送过来的⽂件,当然我举的例⼦⼏乎没有任何显⽰意义,它却给了我们不少的启⽰.
/// 另外如果你的解决⽅案是基于多平台的,那么他们之间的交互最好不要⽤远程应⽤程序接⼝调⽤(RPC),应该尽量⽤基于⽂档的交互, /// ⽐如说下的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("保存成功");
}
}
xml文件怎么打开 /// <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#程序设计有所帮助。
发布评论