用C#把文件转换为XML的代码
时间:2020-12-16 15:37 来源/作者:C#教程网
-
using System;
-
using System.Drawing;
-
using System.Collections;
-
using System.ComponentModel;
-
using System.Windows.Forms;
-
using System.IO;
-
using System.Xml;
-
namespace MyWindows
-
{
-
/// <summary>
-
-
-
-
-
-
-
-
-
-
-
-
public class Form1 : System.Windows.Forms.Form
-
{
-
-
/// <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()
-
{
-
-
-
-
InitializeComponent();
-
-
-
-
-
}
-
-
/// <summary>
-
-
-
protected override void Dispose( bool disposing )
-
{
-
if( disposing )
-
{
-
if(components != null)
-
{
-
components.Dispose();
-
}
-
}
-
base.Dispose( disposing );
-
}
-
-
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
-
/// <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();
-
-
-
-
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);
-
-
-
-
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);
-
-
-
-
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);
-
-
-
-
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);
-
-
-
-
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);
-
-
-
-
this.Load += new System.EventHandler(this.Form1_Load);
-
this.Closed += new System.EventHandler(this.Form1_Closed);
-
-
}
-
#endregion
-
-
/// <summary>
-
-
-
static void Main()
-
{
-
Application.Run(new Form1());
-
}
-
/// <summary>
-
-
-
-
-
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);
-
MessageBox.Show("保存成功");
-
}
-
}
-
-
/// <summary>
-
-
-
-
-
private void button2_Click(object sender, System.EventArgs e)
-
{
-
-
XmlNode node=doc.DocumentElement .SelectSingleNode("me") ;
-
XmlElement ele=(XmlElement)node;
-
string pic=ele.GetAttribute ("aa");
-
-
byte[] bytes=Convert.FromBase64String (pic);
-
-
-
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>
-
-
-
-
-
public void Form1_Load(object sender, System.EventArgs e)
-
{
-
MessageBox.Show("欢迎使用蛙蛙牌文档转换器");
-
}
-
/// <summary>
-
-
-
-
-
public void Form1_Closed(object sender, System.EventArgs e)
-
{
-
mXmlDoc=null;
-
doc=null;
-
}
-
/// <summary>
-
-
-
-
-
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>
-
-
-
-
-
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(strFileName.Length != 0)
-
{
-
doc=new XmlDocument();
-
doc.Load(strFileName);
-
MessageBox.Show("加载成功");
-
}
-
-
}
-
-
}
-
}
相关文章
热门资讯