eXtensible
Markup Language (XML) was designed to transport and store data.
XML
document does not DO anything. It is just information wrapped in tags. Someone
must
write
a piece of software to send, receive or display it.
What is XML?
·
XML stands for EXtensible Markup Language
·
XML is a markup language much like HTML
·
XML was designed to carry data, not to display data
·
XML tags are not predefined. You must define your own tags
·
XML is designed to be self-descriptive
·
XML is a W3C Recommendation.
What can we do with XML?
·
Separates Data from HTML
·
Simplifies Data Sharing
·
Simplifies Data Transport
·
Simplifies Platform Changes
Creating XML
Follow the steps to
make a new project:
1. File
-> New Project -> Visual C# -> Windows Forms Application
2. Add
a TextBox control (textBox1) to your form, and make it Multiline.
Then enlarge it.
3. In
the Using section of your code file add the library using System.Xml;
Add a new button
and paste this code for its onClick method:
// Create the xml document containe
XmlDocument doc = new XmlDocument();
// Create the XML Declaration, and append it to XML
document
XmlDeclaration dec =
doc.CreateXmlDeclaration("1.0",
null, null);
doc.AppendChild(dec);// Create the root element
XmlElement root = doc.CreateElement("Library");
doc.AppendChild(root);
// Create Books
// Note that to set the text inside the element,
// you use .InnerText instead of .Value
//(which will throw an exception).
// You use SetAttribute to set attribute
XmlElement book = doc.CreateElement("Book");
book.SetAttribute("BookType",
"Hardcover");
XmlElement title = doc.CreateElement("Title");
title.InnerText = "Door Number Three";
XmlElement author = doc.CreateElement("Author");
author.InnerText = "O'Leary, Patrick";
book.AppendChild(title);
book.AppendChild(author);
root.AppendChild(book);
book = doc.CreateElement("Book");
book.SetAttribute("BookType",
"Paperback");
title = doc.CreateElement("Title");
title.InnerText = "Lord of Light";
author = doc.CreateElement("Author");
author.InnerText = "Zelanzy, Roger";
book.AppendChild(title);
book.AppendChild(author);
root.AppendChild(book);
string xmlOutput = doc.OuterXml;
textBox1.Text = xmlOutput;
4. This
code represents generation of XML with the class XmlDocument. Click Ctrl+F5 to start
the program.
5. Add
a new button and the following code for its onClick method. This will
show you how you can create XML with the XML Writer.
XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms, wSettings);// Write
Declaration
xw.WriteStartDocument();
// Write the root node
xw.WriteStartElement("Library");
// Write the books and the book elements
xw.WriteStartElement("Book");
xw.WriteStartAttribute("BookType");
xw.WriteString("Hardback");
xw.WriteEndAttribute();
xw.WriteStartElement("Title");
xw.WriteString("Door Number Three");
xw.WriteEndElement();
xw.WriteStartElement("Author");
xw.WriteString("O'Leary, Patrick");
xw.WriteEndElement();
xw.WriteEndElement();
// Write another book
xw.WriteStartElement("Book");
xw.WriteStartAttribute("BookType");
xw.WriteString("Paperback");
xw.WriteEndAttribute();
xw.WriteStartElement("Title");
xw.WriteString("Lord of Light");
xw.WriteEndElement();
xw.WriteStartElement("Author");
xw.WriteString("Zelanzy, Roger");
xw.WriteEndElement();
xw.WriteEndElement();
// Close the document
xw.WriteEndDocument();
// Flush the write
xw.Flush();
Byte[] buffer = new Byte[ms.Length];
buffer = ms.ToArray();
string xmlOutput =
System.Text.Encoding.UTF8.GetString(buffer);
textBox1.Text = xmlOutput;
6. Add
library using System.IO; and start the application
so you can see the differences between the two methods.
7. There
is a third way to generate XML and it can be used in .NET Framework 3.5 and above.
For it you will need to add the library using
System.Xml.Linq; and again a new button with the following onClick code:
//XDocument
MessageBox.Show(new XElement("Foo",
new XAttribute("Bar", "some value"),
new XElement("Nested1", "data"),
new XElement("Nested2", "other data")
).ToString());
//XmlDocument
XmlDocument doc = new XmlDocument();
XmlElement
el=(XmlElement)doc.AppendChild(doc.CreateElement("Foo"));
el.SetAttribute("Bar", "some value");
el.AppendChild(doc.CreateElement("Nested1")).InnerText
= "data";
el.AppendChild(doc.CreateElement("Nested2")).InnerText="other
data";
MessageBox.Show(doc.OuterXml.ToString());
8. Start
the program and test button 3. XDocument is easy to use, but when you need to
work with big amounts of data it is better to use XML Writer.
Reading XML
9. Download
and save the following file ( cd_catalog.xml )
into your Bin/Debug folder of
your project.
10. Add
a button “Read XML” and in its onClick method add the code:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("cd_catalog.xml");
XmlNodeList name =
xDoc.GetElementsByTagName("TITLE");
XmlNodeList age =
xDoc.GetElementsByTagName("ARTIST");
//XmlNodeList represents an ordered collection of nodes.
//Display the results in a message box.
string xmlText = "";
for (int i = 0; i < name.Count; i++)
{
xmlText += "Title: " + name[i].InnerText +
"\t Artist: " + age[i].InnerText
+"\n";
}
MessageBox.Show(xmlText);
11. Start
the code and see the result. You can see that the file can be read element by
element.
12. To
show all of the data add a DataGridView control to your form. Add another
button and the following code for it:
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("cd_catalog.xml");
DataSet ds = new DataSet("CD DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "CD";
13. Start
with CTRL+F5.
No comments:
Post a Comment