Lesson 17
Lesson 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
- 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>
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?