• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > 用C#把文件转换为XML的代码

用C#把文件转换为XML的代码

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了c#xml文件读写,c#读取xml文件,c#创建xml文件,c#生成xml文件,c#解析xml文件等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
  /**//// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </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);
   //
   //手工注册一下Load和Closed事件
   //
   this.Load += new System.EventHandler(this.Form1_Load);
   this.Closed += new System.EventHandler(this.Form1_Closed);

  }
  #endregion

  /**//// <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="s

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 读写XML文件的内容并将其显示在ListView控件上的方法
  • C#代码操作XML进行增、删、改操作
  • C#简单读写txt文件的方法
  • C#实现的xml操作类完整实例
  • C# XML序列化方法及常用特性总结分析
  • C#进行文件读写、创建、复制、移动、删除的方法
  • C#自定义处理xml数据类实例
  • C#递归读取XML菜单数据的方法
  • C#实现提高xml读写速度的方法
  • C#实现对象XML序列化的方法

相关文章

  • 2017-05-28C#隐藏控制台键盘输入的方法
  • 2017-05-28C#之WinForm WebBrowser实用技巧汇总
  • 2017-05-28C#最简单的关闭子窗体更新父窗体的实现方法
  • 2017-05-28C# 判断字符为空的6种方法的效率实测对比
  • 2017-05-28C#读写文件的方法汇总
  • 2017-05-28C#文字换行的实现方法
  • 2017-05-28c# HttpWebRequest通过代理服务器抓取网页内容应用介绍
  • 2017-05-28C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • 2017-05-28C#生成随机数的方法小结
  • 2017-05-28C#队列Queue用法实例分析

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C# double和decimal数据类型以截断的方式保留指定的小数位数
    • C#使用Ado.Net更新和添加数据到Excel表格的方法
    • C#控制Excel Sheet使其自适应页宽与列宽的方法
    • c#获取当前年的周数及当前月的天数示例代码
    • C#面向对象特征的具体实现及作用详解
    • C#代码设置开机启动示例
    • c#与mysql的连接
    • C#集合遍历时删除和增加元素的方法
    • 基于运算符重载的那些事
    • C# Pointer指针应用实例简述

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有