当前位置:首页 > 通信资讯 > 正文

c#读写xml文件(c# 生成xml)

  1. usingSystem;
  2. usingSystem.Drawing;
  3. usingSystem.Collections;
  4. usingSystem.ComponentModel;
  5. usingSystem.Windows.Forms;
  6. usingSystem.IO;
  7. usingSystem.Xml;
  8. namespaceMyWindows
  9. {
  10.  /**////<summary>
  11.  ///这个示例演示如何把Office文件编码为xml文件以及如何把生成的xml文件转换成Office文件
  12.  ///把文件转换成xml格式,然后就可以用web服务,.NETRemoting,WinSock等传送了(其中后两者可以不转换也可以传送)
  13.  ///xml解决了在多层架构中数据传输的问题,比如说在客户端可以用Web服务获取服务器端的office文件,修改后再回传给服务器
  14.  ///只要把文件转换成xml格式,便有好多方案可以使用了,而xml具有平台无关性,你可以在服务端用.net用发布web服务,然后客户端用
  15.  ///Java写一段applit小程序来处理发送过来的文件,当然我举的例子几乎没有任何显示意义,它却给了我们不少的启示.
  16.  ///另外如果你的解决方案是基于多平台的,那么他们之间的交互最好不要用远程应用程序接口调用(RPC),应该尽量用基于文档的交互,
  17.  ///比如说.net下的MSMQ,j2ee的JMQ.
  18.  ///
  19.  ///示例中设计到好多的类,我并没有在所有的地方做过多注释,有不明白的地方请参阅MSDN,这是偶第一个windows程序,有不对的地方
  20.  ///欢迎各位指导
  21.  ///</summary>
  22.  publicclassForm1:System.Windows.Forms.Form
  23.  {
  24.   /**////<summary>
  25.   ///声明四个Button,一个OpenFileDialog,一个SaveFileDialog,以及两个XmlDocument
  26.   ///</summary>
  27.   privateSystem.Windows.Forms.Buttonbutton1;
  28.   privateSystem.Windows.Forms.Buttonbutton2;
  29.   privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;
  30.   privateSystem.Windows.Forms.SaveFileDialogsaveFileDialog1;
  31.   privateSystem.Windows.Forms.Buttonbutton3;
  32.   privateSystem.Windows.Forms.Buttonbutton4;
  33.   privateSystem.Xml.XmlDocumentmXmlDoc;
  34.   privateSystem.Xml.XmlDocumentdoc;
  35.   privateSystem.ComponentModel.Containercomponents=null;
  36.   publicForm1()
  37.   {
  38.    //
  39.    //Windows窗体设计器支持所必需的
  40.    //
  41.    InitializeComponent();
  42.    //
  43.    //TODO:在InitializeComponent调用后添加任何构造函数代码
  44.    //
  45.   }
  46.   /**////<summary>
  47.   ///清理所有正在使用的资源。
  48.   ///</summary>
  49.   protectedoverridevoidDispose(booldisposing)
  50.   {
  51.    if(disposing)
  52.    {
  53.     if(components!=null)
  54.     {
  55.      components.Dispose();
  56.     }
  57.    }
  58.    base.Dispose(disposing);
  59.   }
  60.   Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码
  61.   /**////<summary>
  62.   ///设计器支持所需的方法-不要使用代码编辑器修改
  63.   ///此方法的内容。
  64.   ///</summary>
  65.   privatevoidInitializeComponent()
  66.   {
  67.    this.button1=newSystem.Windows.Forms.Button();
  68.    this.button2=newSystem.Windows.Forms.Button();
  69.    this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
  70.    this.saveFileDialog1=newSystem.Windows.Forms.SaveFileDialog();
  71.    this.button3=newSystem.Windows.Forms.Button();
  72.    this.button4=newSystem.Windows.Forms.Button();
  73.    this.SuspendLayout();
  74.    //
  75.    //button1
  76.    //
  77.    this.button1.Location=newSystem.Drawing.Point(96,32);
  78.    this.button1.Name="button1";
  79.    this.button1.TabIndex=0;
  80.    this.button1.Text="生成xml";
  81.    this.button1.Click+=newSystem.EventHandler(this.button1_Click);
  82.    //
  83.    //button2
  84.    //
  85.    this.button2.Location=newSystem.Drawing.Point(96,80);
  86.    this.button2.Name="button2";
  87.    this.button2.TabIndex=1;
  88.    this.button2.Text="生成doc";
  89.    this.button2.Click+=newSystem.EventHandler(this.button2_Click);
  90.    //
  91.    //button3
  92.    //
  93.    this.button3.Location=newSystem.Drawing.Point(8,32);
  94.    this.button3.Name="button3";
  95.    this.button3.TabIndex=2;
  96.    this.button3.Text="加载doc";
  97.    this.button3.Click+=newSystem.EventHandler(this.button3_Click);
  98.    //
  99.    //button4
  100.    //
  101.    this.button4.Location=newSystem.Drawing.Point(8,80);
  102.    this.button4.Name="button4";
  103.    this.button4.TabIndex=3;
  104.    this.button4.Text="加载xml";
  105.    this.button4.Click+=newSystem.EventHandler(this.button4_Click);
  106.    //
  107.    //Form1
  108.    //
  109.    this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
  110.    this.ClientSize=newSystem.Drawing.Size(184,141);
  111.    this.Controls.Add(this.button4);
  112.    this.Controls.Add(this.button3);
  113.    this.Controls.Add(this.button2);
  114.    this.Controls.Add(this.button1);
  115.    this.Name="Form1";
  116.    this.Text="Form1";
  117.    this.ResumeLayout(false);
  118.    //
  119.    //手工注册一下Load和Closed事件
  120.    //
  121.    this.Load+=newSystem.EventHandler(this.Form1_Load);
  122.    this.Closed+=newSystem.EventHandler(this.Form1_Closed);
  123.   }
  124.   #endregion
  125.   /**////<summary>
  126.   ///从这个入口启动窗体
  127.   ///</summary>
  128.   staticvoidMain()
  129.   {
  130.    Application.Run(newForm1());
  131.   }
  132.   /**////<summary>
  133.   ///把加载的Office文件转换为xml文件
  134.   ///</summary>
  135.   ///<paramname="sender"></param>
  136.   ///<paramname="e"></param>
  137.   privatevoidbutton1_Click(objectsender,System.EventArgse)
  138.   {
  139.    saveFileDialog1.Filter="xml文件|*.xml";//设置打开对话框的文件过滤条件
  140.    saveFileDialog1.Title="保存成xml文件";//设置打开对话框的标题
  141.    saveFileDialog1.FileName="";
  142.    saveFileDialog1.ShowDialog();//打开对话框
  143.    if(saveFileDialog1.FileName!="")//检测用户是否输入了保存文件名
  144.    {
  145.     mXmlDoc.Save(saveFileDialog1.FileName);//用私有对象mXmlDoc保存文件,mXmlDoc在前面声明过
  146.     MessageBox.Show("保存成功");
  147.    }
  148.   }
  149.   /**////<summary>
  150.   ///把加载的xml文件转换为Office文件
  151.   ///</summary>
  152.   ///<paramname="sender"></param>
  153.   ///<paramname="e"></param>
  154.   privatevoidbutton2_Click(objectsender,System.EventArgse)
  155.   {
  156.    //从私有对象dox里选取me节点,这里的一些对xml对象的操作详细说明可以参考msdn以获取更多信息
  157.    XmlNodenode=doc.DocumentElement.SelectSingleNode("me");
  158.    XmlElementele=(XmlElement)node;//获取一个xml元素
  159.    stringpic=ele.GetAttribute("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic
  160.    byte[]bytes=Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
  161.   
  162.    //从保存对话框里获取文件保存地址
  163.    saveFileDialog1.Filter="OfficeDocuments(*.doc,*.xls,*.ppt)|*.doc;*.xls;*.ppt";
  164.    saveFileDialog1.Title="保存成office文件";
  165.    saveFileDialog1.FileName="";
  166.    saveFileDialog1.ShowDialog();
  167.    if(saveFileDialog1.FileName!="")
  168.    {
  169.     //创建文件流并保存
  170.     FileStreamoutfile=newSystem.IO.FileStream(saveFileDialog1.FileName,System.IO.FileMode.CreateNew);
  171.     outfile.Write(bytes,0,(int)bytes.Length);
  172.     MessageBox.Show("保存成功");
  173.    }
  174.   }
  175.   /**////<summary>
  176.   ///加载窗口时的一些初始化行为
  177.   ///</summary>
  178.   ///<paramname="sender"></param>
  179.   ///<paramname="e"></param>
  180.   publicvoidForm1_Load(objectsender,System.EventArgse)
  181.   {
  182.    MessageBox.Show("欢迎使用蛙蛙牌文档转换器");
  183.   }
  184.   /**////<summary>
  185.   ///卸载窗体时把临时变量全部释放
  186.   ///</summary>
  187.   ///<paramname="sender"></param>
  188.   ///<paramname="e"></param>
  189.   publicvoidForm1_Closed(objectsender,System.EventArgse)
  190.   {
  191.    mXmlDoc=null;
  192.    doc=null;
  193.   }
  194.   /**////<summary>
  195.   ///加载office文件并编码序列花为一个XmlDocument变量
  196.   ///</summary>
  197.   ///<paramname="sender"></param>
  198.   ///<paramname="e"></param>
  199.   privatevoidbutton3_Click(objectsender,System.EventArgse)
  200.   {
  201.    stringstrFileName;
  202.    openFileDialog1.Filter="OfficeDocuments(*.doc,*.xls,*.ppt)|*.doc;*.xls;*.ppt";
  203.    openFileDialog1.FilterIndex=1;
  204.    openFileDialog1.FileName="";
  205.    openFileDialog1.ShowDialog();
  206.    strFileName=openFileDialog1.FileName;
  207.    if(strFileName.Length!=0)
  208.    {
  209.     System.IO.FileStreaminFile=newFileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
  210.     byte[]binaryData=newbyte[inFile.Length];
  211.     inFile.Read(binaryData,0,(int)inFile.Length);
  212.     stringmStr=Convert.ToBase64String(binaryData);
  213.     stringhh=mStr;
  214.     mXmlDoc=newSystem.Xml.XmlDocument();
  215.  
  216.     mStr=string.Format("<wawa><meaa=\"{0}\"/></wawa>",mStr);
  217.     mXmlDoc.LoadXml(mStr);
  218.     MessageBox.Show("加载成功");
  219.    }
  220.   }
  221.   /**////<summary>
  222.   ///加载xml文件到私有对象dox
  223.   ///</summary>
  224.   ///<paramname="sender"></param>
  225.   ///<paramname="e"></param>
  226.   privatevoidbutton4_Click(objectsender,System.EventArgse)
  227.   {
  228.    stringstrFileName;
  229.    openFileDialog1.Filter="xml文件|*.xml";
  230.    openFileDialog1.FilterIndex=1;
  231.    openFileDialog1.FileName="";
  232.    openFileDialog1.ShowDialog();
  233.    strFileName=openFileDialog1.FileName;
  234.    //Iftheuserdoesnotcancel,openthedocument.
  235.    if(strFileName.Length!=0)
  236.    {
  237.     doc=newXmlDocument();
  238.     doc.Load(strFileName);
  239.     MessageBox.Show("加载成功");
  240.    }
  241.   }
  242.  }
  243. }
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。