本文实例讲述了C#实现将文件转换为XML的方法。分享给大家供大家参考,具体如下:
usingSystem; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.IO; usingSystem.Xml; namespaceMyWindows { ///<summary> ///这个示例演示如何把Office文件编码为xml文件以及如何把生成的xml文件转换成Office文件 ///把文件转换成xml格式,然后就可以用web服务,.NETRemoting,WinSock等传送了(其中后两者可以不转换也可以传送) ///xml解决了在多层架构中数据传输的问题,比如说在客户端可以用Web服务获取服务器端的office文件,修改后再回传给服务器 ///只要把文件转换成xml格式,便有好多方案可以使用了,而xml具有平台无关性,你可以在服务端用.net用发布web服务,然后客户端用 ///Java写一段applit小程序来处理发送过来的文件,当然我举的例子几乎没有任何显示意义,它却给了我们不少的启示. ///另外如果你的解决方案是基于多平台的,那么他们之间的交互最好不要用远程应用程序接口调用(RPC),应该尽量用基于文档的交互, ///比如说.net下的MSMQ,j2ee的JMQ. /// ///示例中设计到好多的类,我并没有在所有的地方做过多注释,有不明白的地方请参阅MSDN,这是偶第一个windows程序,有不对的地方 ///欢迎各位指导 ///</summary> publicclassForm1:System.Windows.Forms.Form { ///<summary> ///声明四个Button,一个OpenFileDialog,一个SaveFileDialog,以及两个XmlDocument ///</summary> privateSystem.Windows.Forms.Buttonbutton1; privateSystem.Windows.Forms.Buttonbutton2; privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1; privateSystem.Windows.Forms.SaveFileDialogsaveFileDialog1; privateSystem.Windows.Forms.Buttonbutton3; privateSystem.Windows.Forms.Buttonbutton4; privateSystem.Xml.XmlDocumentmXmlDoc; privateSystem.Xml.XmlDocumentdoc; privateSystem.ComponentModel.Containercomponents=null; publicForm1() { // //Windows窗体设计器支持所必需的 // InitializeComponent(); // //TODO:在InitializeComponent调用后添加任何构造函数代码 // } ///<summary> ///清理所有正在使用的资源。 ///</summary> protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } Windows窗体设计器生成的代码 ///<summary> ///从这个入口启动窗体 ///</summary> staticvoidMain() { Application.Run(newForm1()); } ///<summary> ///把加载的Office文件转换为xml文件 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton1_Click(objectsender,System.EventArgse) { 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> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton2_Click(objectsender,System.EventArgse) { //从私有对象dox里选取me节点,这里的一些对xml对象的操作详细说明可以参考msdn以获取更多信息 XmlNodenode=doc.DocumentElement.SelectSingleNode("me"); XmlElementele=(XmlElement)node;//获取一个xml元素 stringpic=ele.GetAttribute("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic byte[]bytes=Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流 //从保存对话框里获取文件保存地址 saveFileDialog1.Filter="OfficeDocuments(*.doc,*.xls,*.ppt)|*.doc;*.xls;*.ppt"; saveFileDialog1.Title="保存成office文件"; saveFileDialog1.FileName=""; saveFileDialog1.ShowDialog(); if(saveFileDialog1.FileName!="") { //创建文件流并保存 FileStreamoutfile=newSystem.IO.FileStream(saveFileDialog1.FileName,System.IO.FileMode.CreateNew); outfile.Write(bytes,0,(int)bytes.Length); MessageBox.Show("保存成功"); } } ///<summary> ///加载窗口时的一些初始化行为 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> publicvoidForm1_Load(objectsender,System.EventArgse) { MessageBox.Show("欢迎使用蛙蛙牌文档转换器"); } ///<summary> ///卸载窗体时把临时变量全部释放 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> publicvoidForm1_Closed(objectsender,System.EventArgse) { mXmlDoc=null; doc=null; } ///<summary> ///加载office文件并编码序列花为一个XmlDocument变量 ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton3_Click(objectsender,System.EventArgse) { stringstrFileName; openFileDialog1.Filter="OfficeDocuments(*.doc,*.xls,*.ppt)|*.doc;*.xls;*.ppt"; openFileDialog1.FilterIndex=1; openFileDialog1.FileName=""; openFileDialog1.ShowDialog(); strFileName=openFileDialog1.FileName; if(strFileName.Length!=0) { System.IO.FileStreaminFile=newFileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read); byte[]binaryData=newbyte[inFile.Length]; inFile.Read(binaryData,0,(int)inFile.Length); stringmStr=Convert.ToBase64String(binaryData); stringhh=mStr; mXmlDoc=newSystem.Xml.XmlDocument(); mStr=string.Format("<wawa><meaa="{0}"/></wawa>",mStr); mXmlDoc.LoadXml(mStr); MessageBox.Show("加载成功"); } } ///<summary> ///加载xml文件到私有对象dox ///</summary> ///<paramname="sender"></param> ///<paramname="e"></param> privatevoidbutton4_Click(objectsender,System.EventArgse) { stringstrFileName; openFileDialog1.Filter="xml文件|*.xml"; openFileDialog1.FilterIndex=1; openFileDialog1.FileName=""; openFileDialog1.ShowDialog(); strFileName=openFileDialog1.FileName; //Iftheuserdoesnotcancel,openthedocument. if(strFileName.Length!=0) { doc=newXmlDocument(); doc.Load(strFileName); MessageBox.Show("加载成功"); } } } }
希望本文所述对大家C#程序设计有所帮助。
您可能感兴趣的文章:详解C#借助.NET框架中的XmlTextReader类读取XML的方法C#程序中使用LINQtoXML来查询XML格式数据的实例C#获取远程XML文档的方法C#保存与读取DataTable信息到XML格式的方法C#实现基于XML配置MenuStrip菜单的方法C#实现xml文件的读取与写入简单实例C#中基于流的XML文件操作笔记C#读取xml节点数据方法小结c#使用Dataset读取XML文件动态生成菜单的方法C#从文件流读取xml文件到DataSet并显示的方法C#中使用XmlDocument类来创建和修改XML格式的数据文件
浅谈C++模板元编程
C++实现循环队列和链式队列的示例
上述就是C#学习教程:C#实现将文件转换为XML的方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/906939.html