Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lesson 17

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Lesson 17

XML
eXtensible Markup
Language
- designed to carry data, not to display data
- you must define your own tags
- designed to be self-descriptive
- Does Not DO Anything
- Simplifies Data Sharing
XML
XML
<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="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
XML
- tags are case sensitive
- tags must have closing tag
- elements must be properly nested
- documents must contain one element that is
the parent of all other elements
- attribute values must always be quoted
XML
- entity reference

<message>if salary < 1000 then</message>


&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
XML & C#
XmlReader xmlReader =
XmlReader.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
while(xmlReader.Read())
{
if((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name
== "Cube"))
{
if(xmlReader.HasAttributes)
Console.WriteLine(xmlReader.GetAttribute("currency") + ": " +
xmlReader.GetAttribute("rate"));
}
}
Console.ReadKey();
http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
XML & C#
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
foreach(XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes)
Console.WriteLine(xmlNode.Attributes["currency"].Value + ": " + xmlNode.Attributes["rate"].Value);
Console.ReadKey();
XmlNode
- Name property
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<user name=\"John Doe\">A user node</user>");
Console.WriteLine(xmlDoc.DocumentElement.Name);
Console.ReadKey();

- InnerText property
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<test>InnerText is here</test>");
Console.WriteLine(xmlDoc.DocumentElement.InnerText);
Console.ReadKey();
XmlNode
- InnerXml property
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<users><user>InnerText/InnerXml is here</user></users>");
Console.WriteLine("InnerXml: " + xmlDoc.DocumentElement.InnerXml);
Console.WriteLine("InnerText: " + xmlDoc.DocumentElement.InnerText);
Console.ReadKey();

- OuterXml property
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<users><user>InnerText/InnerXml is here</user></users>");
Console.WriteLine("InnerXml: " + xmlDoc.DocumentElement.InnerXml);
Console.WriteLine("OuterXml: " + xmlDoc.DocumentElement.OuterXml);
Console.ReadKey();
XPath
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://rss.cnn.com/rss/edition_world.rss");
XmlNode titleNode =
xmlDoc.SelectSingleNode("//channel/title");
if(titleNode != null)
Console.WriteLine(titleNode.InnerText);
Console.ReadKey();

http://rss.cnn.com/rss/edition_world.rss
XPath
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("http://rss.cnn.com/rss/edition_world.rss");
XmlNodeList itemNodes = xmlDoc.SelectNodes("//channel/item");
foreach(XmlNode itemNode in itemNodes)
{
XmlNode titleNode = itemNode.SelectSingleNode("title");
XmlNode dateNode = itemNode.SelectSingleNode("pubDate");
if((titleNode != null) && (dateNode != null))
Console.WriteLine(dateNode.InnerText + ": " + titleNode.InnerText);
}
Console.ReadKey();
XmlWriter
XmlWriter xmlWriter = XmlWriter.Create("test.xml");

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("users");
xmlWriter.WriteStartElement("user");
xmlWriter.WriteAttributeString("age", "42");
xmlWriter.WriteString("John Doe");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("user");
xmlWriter.WriteAttributeString("age", "39");
xmlWriter.WriteString("Jane Doe");
xmlWriter.WriteEndDocument();
xmlWriter.Close();
XmlDocument writing
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("users");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("user");
XmlAttribute attribute = xmlDoc.CreateAttribute("age");
attribute.Value = "42";
userNode.Attributes.Append(attribute);
userNode.InnerText = "John Doe";
rootNode.AppendChild(userNode);
userNode = xmlDoc.CreateElement("user");
attribute = xmlDoc.CreateAttribute("age");
attribute.Value = "39";
userNode.Attributes.Append(attribute);
userNode.InnerText = "Jane Doe";
rootNode.AppendChild(userNode);
xmlDoc.Save("test-doc.xml");
XmlDocument writing
<users>
<user age="42">John Doe</user>
<user age="39">Jane Doe</user>
</users>

XmlDocument xmlDoc = new XmlDocument();


xmlDoc.Load("test-doc.xml");
XmlNodeList userNodes = xmlDoc.SelectNodes("//users/user");
foreach(XmlNode userNode in userNodes){
int age = int.Parse(userNode.Attributes["age"].Value);
userNode.Attributes["age"].Value = (age + 1).ToString();
}
xmlDoc.Save("test-doc.xml");
Task
Create a console application that will allow you to read and
write in XML file that stores information about students.
Each student should have information about name,
surname, index number and subjects. Each subject
should have the name of the class, the current status
(listening, passed or failed) and if passed should be
assigned a grade.
The application should provide a way that you can find a
specific student by index and show all the classes for
that student on screen.
Extra tasks!!!

1. A palindromic number reads the same both ways. The largest palindrome
made from the product of two 2-digit numbers is 9009 = 91x99. Find the
largest palindrome made from the product of two 3-digit numbers.

2. 2520 is the smallest number that can be divided by each of the numbers
from 1 to 10 without any remainder. What is the smallest positive number
that is evenly divisible by all of the numbers from 1 to 20?

You might also like