Showing posts with label Windows Applications. Show all posts
XMLWriter
XMLWriter class is used to create and xml file.
Below example is used create xml
file in the bin location
using System.Xml;
class Program
{
class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;
public Employee(int id, string
firstName, string lastName, int salary)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = salary;
}
public int Id { get { return _id; } }
public string FirstName { get { return _firstName; } }
public string LastName { get { return _lastName; } }
public int Salary { get { return _salary; } }
}
static void Main()
{
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
employees[2] = new Employee(4, "Norah", "Miller", 20000);
employees[3] = new Employee(12, "Cecil", "Walker", 120000);
using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
foreach (Employee employee in employees)
{
writer.WriteStartElement("Employee");
writer.WriteElementString("ID", employee.Id.ToString());
writer.WriteElementString("FirstName", employee.FirstName);
writer.WriteElementString("LastName", employee.LastName);
writer.WriteElementString("Salary", employee.Salary.ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
XML data
<Employees>
<Employee>
<ID>1</ID>
<FirstName>David</FirstName>
<LastName>Smith</LastName>
<Salary>10000</Salary>
</Employee>
<Employee>
<ID>3</ID>
<FirstName>Mark</FirstName>
<LastName>Drinkwater</LastName>
<Salary>30000</Salary>
</Employee>
<Employee>
<ID>4</ID>
<FirstName>Norah</FirstName>
<LastName>Miller</LastName>
<Salary>20000</Salary>
</Employee>
<Employee>
<ID>12</ID>
<FirstName>Cecil</FirstName>
<LastName>Walker</LastName>
<Salary>120000</Salary>
</Employee>
XMLReader
XMLReader class is used to read the xml data
Below example is used to read data
from xml file
ReadText.xml
<?xml version="1.0" encoding="utf-8" ?>
<Header>
<article
name="backgroundworker">
Example text.
</article>
<article
name="threadpool">
More text.
</article>
<article></article>
<article>Final
text.</article>
</Header>
Output
Start <Header> element.
Start <article> element.
Has
attribute name: backgroundworker
Text node: Example text.
Start <article> element.
Has
attribute name: threadpool
Text node: More text.
Start <article> element.
Text node:
Start <article> element.
Text node: Final text.
Code
using System;
using System.Xml;
class Program
{
static void Main()
{
// Create an XML reader for this
file.
using (XmlReader reader = XmlReader.Create("ReadText.xml"))
{
while (reader.Read())
{
// Only detect start elements.
if (reader.IsStartElement())
{
// Get element
name and switch on it.
switch (reader.Name)
{
case "Header":
// Detect this element.
Console.WriteLine("Start <Header>
element.");
break;
case "article":
// Detect this article element.
Console.WriteLine("Start <article>
element.");
// Search for the attribute name
on this current node.
string attribute = reader["name"];
if (attribute != null)
{
Console.WriteLine(" Has attribute name: " + attribute);
}
// Next read will contain text.
if (reader.Read())
{
Console.WriteLine(" Text node: " + reader.Value.Trim());
}
break;
}
}
}
}
Console.ReadKey();
}
XML Basics
XML stands for EXtensible Markup Language.
XML was designed to store and transport data.
XML was designed to be both human- and machine-readable.
.xml is the extension for xml documents.
XML was designed to store and transport data.
XML was designed to be both human- and machine-readable.
.xml is the extension for xml documents.
Creating XML document
The following data can be written in xml format as shown
below.
Data:
Note
To: Tove
From: Jani
Happy weekend
From: Jani
Happy weekend
XML Format:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Happy weekendbody>
</note>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Happy weekendbody>
</note>
XML Example to store
books information
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
