Sunteți pe pagina 1din 6

Aim:- Demonstrate hoe to retrieve information from an execting XML document

and how to create a new XML document.

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();

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.fileName = new System.Windows.Forms.Label();
this.XMLOutput = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.fileName.AutoSize = true;
this.fileName.Location = new System.Drawing.Point(24, 16);
this.fileName.Name = "fileName";
this.fileName.Size = new System.Drawing.Size(48, 13);
this.fileName.TabIndex = 1;
this.fileName.Text = "fileName";
this.XMLOutput.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles
.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.XMLOutput.Location = new System.Drawing.Point(8, 48);
this.XMLOutput.Multiline = true;
this.XMLOutput.Name = "XMLOutput";
this.XMLOutput.Size = new System.Drawing.Size(776, 312);
this.XMLOutput.TabIndex = 2;
this.XMLOutput.TextChanged += new
System.EventHandler(this.XMLOutput_TextChanged);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(792, 374);
this.Controls.Add(this.XMLOutput);
this.Controls.Add(this.fileName);
this.Name = "MainForm";
this.Text = "MainForm";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
static void Main()
{
Application.Run(new MainForm());
}
private void MainForm_Load(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 + "\\customers.xml";
}
else
{
XmlFile = directoryInfo.FullName + "\\customers.xml";
}
fileName.Text = XmlFile;
XmlTextReader XmlRdr = new System.Xml.XmlTextReader(XmlFile);
while(XmlRdr.Read())
{
if (XmlRdr.NodeType==XmlNodeType.Element&&XmlRdr.Name=="name")
{
XMLOutput.Text += XmlRdr.ReadString() + "\r\n";
}
}
}

private void XMLOutput_TextChanged(object sender, EventArgs e)


{

}
}
}
Output:-

Result:- The programme is executed successfully without any errors.

S-ar putea să vă placă și