Documente Academic
Documente Profesional
Documente Cultură
Source code:-
1.Xml Writer:-
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
namespace XMLTextWriter
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox OutputFileName;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Button WriteFile;
private System.ComponentModel.Container components = null;
public MainForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.OutputFileName = new System.Windows.Forms.TextBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.WriteFile = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
this.label1.Text = "File Name";
this.OutputFileName.Location = new System.Drawing.Point(144, 24);
this.OutputFileName.Name = "OutputFileName";
this.OutputFileName.Size = new System.Drawing.Size(464, 20);
this.OutputFileName.TabIndex = 1;
this.OutputFileName.Text = "customers.xml";
this.statusBar1.Location = new System.Drawing.Point(0, 416);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(704, 22);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = "statusBar1";
this.WriteFile.Location = new System.Drawing.Point(144, 56);
this.WriteFile.Name = "WriteFile";
this.WriteFile.Size = new System.Drawing.Size(75, 23);
this.WriteFile.TabIndex = 3;
this.WriteFile.Text = "Write File";
this.WriteFile.Click += new System.EventHandler(this.WriteFile_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(704, 438);
this.Controls.Add(this.WriteFile);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.OutputFileName);
this.Controls.Add(this.label1);
this.Name = "MainForm";
this.Text = "Xml Writer";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
static void Main()
{
Application.Run(new MainForm());
}
private void WriteFile_Click(object sender, System.EventArgs e)
{
string XmlFile;
System.IO.DirectoryInfo directoryInfo;
System.IO.DirectoryInfo directoryXML;
directoryInfo = System.IO.Directory.GetParent(Application.StartupPath);
if(directoryInfo.Name.ToString() == "bin")
{
directoryXML = System.IO.Directory.GetParent(directoryInfo.FullName);
XmlFile = directoryXML.FullName + "\\" + OutputFileName.Text;
}
else
{
XmlFile = directoryInfo.FullName + "\\" + OutputFileName.Text;
}
XmlTextWriter XmlWtr = new System.Xml.XmlTextWriter(XmlFile,null);
XmlWtr.Formatting=Formatting.Indented;
XmlWtr.WriteStartDocument();
XmlWtr.WriteStartElement("customers");
XmlWtr.WriteStartElement("customer");
XmlWtr.WriteElementString("name", "Kevin Anders");
XmlWtr.WriteElementString("phone", "555.555.5555");
XmlWtr.WriteEndElement();
XmlWtr.WriteStartElement("customer");
XmlWtr.WriteElementString("name", "Staci Richard");
XmlWtr.WriteElementString("phone", "555.122.1552");
XmlWtr.WriteEndElement();
XmlWtr.WriteEndElement();
XmlWtr.WriteEndDocument();
XmlWtr.Flush();
XmlWtr.Close();
statusBar1.Text = "Output file has been written";
}
private void MainForm_Load(object sender, EventArgs e)
{ }
}
}
Output:-
2.Xml Reader:-
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
namespace XMLTextReader
{
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label fileName;
private System.Windows.Forms.TextBox XMLOutput;
private System.ComponentModel.Container components = null;
public MainForm()
{
InitializeComponent();
}
}
}
Output:-