SlideShare a Scribd company logo
1 of 53
Svetlin Nakov
Telerik Software Academy
http://academy.telerik.com
ManagerTechnicalTraining
www.nakov.com
Processing XML in .NET
DOM Parser, Streaming Parser, XPath, LINQ to XML
Table of Contents
1. Processing XML documents in .NET
 Using the DOM Parser
 Using the Streaming Parser
2. Using XPath to Search in XML Documents
3. Using LINQ-To-XML
4. XSLTransformation in .NET
2
The DOM Parser
Parsing XML Documents as DOMTrees
The DOM Parser
 The following XML document is given:
4
<?xml version="1.0"?>
<library name=".NET Developer's Library">
<book>
<title>Professional C# 4.0 and .NET 4</title>
<author>Christian Nagel</author>
<isbn>978-0-470-50225-9</isbn>
</book>
<book>
<title>Silverlight in Action</title>
<author>Pete Brown</author>
<isbn>978-1-935182-37-5</isbn>
</book>
</library>
The DOM Parser (2)
 This document is represented in the in the
memory as a DOM tree in the following way:
5
document
?xml library
version 1.0 encoding utf-8 book
title author isbn
Professional... Chris... 978-0-470...
book
title author isbn
Silverlight... Pete Brown 978-1...
name .NET Developer’s...
Header part
(prolog)
Root node
The DOM Parser – Example
6
XmlDocument doc = new XmlDocument();
doc.Load("library.xml");
XmlNode rootNode = doc.DocumentElement;
Console.WriteLine("Root node: {0}", rootNode.Name);
foreach (XmlAttribute atr in rootNode.Attributes)
{
Console.WriteLine("Attribute: {0}={1}",
atr.Name, atr.Value);
}
foreach (XmlNode node in rootNode.ChildNodes)
{
Console.WriteLine("nBook title = {0}",
node["title"].InnerText);
Console.WriteLine("Book author = {0}",
node["author"].InnerText);
Console.WriteLine("Book isbn = {0}",
node["isbn"].InnerText);
}
Parsing XML Document
with the DOM Parser
Live Demo
Classes to Work with DOM
 The DOM parser provides few important
classes:
 XmlDocument
 Represents the DOM tree
 Usually contains two elements:
 Header part (prolog)
 The root element of the XML document
 XmlNode
 Abstract base class for all nodes in a DOM tree
8
Classes to Work with DOM (2)
 XmlElement
 Represents a XML element
 XmlAttribute
 Represents an attribute of an XML tag (couple
name-value)
 XmlAttributeCollection
 List of XML attributes attached to an element
 XmlNodeList
 List of XML DOM nodes
9
The XmlNode Class
 The class System.Xml.XmlNode:
 Fundamental for the DOM processing
 Represents a base node
 Its inheritor-classes are:
 XmlDocument, XmlElement, XmlAttribute,
XmlDeclaration, …
 Allows navigation in the DOM tree:
 ParentNode – returns the parent node (null
for the root)
10
The XmlNode Class (2)
 PreviousSibling / NextSibling – returns
the left / right node to the current
 FirstChild / LastChild – returns the first /
last child of the current node
 Item (indexer [] in C#) – returns the child of the
current node by its name
 Working with the current node:
 Name – returns the name of the node (element,
attribute …)
 Value – gets the node value
11
The XmlNode Class (3)
 Attributes – returns the node attributes as
XmlAttributeCollection
 InnerXml, OuterXml – returns the part of the
XML containing the current node
 Respectively with or without the node itself
 InnerText – concatenation of the values of
the node and its inheritors
 NodeType – returns the node type
12
The XmlNode Class (4)
 Changing of the current node:
 AppendChild(…) / PrependChild(…)
 Inserts new child after / before all other children
of the current node
 InsertBefore(…) / InsertAfter(…)
 Inserts new child before / after given inheritor
 RemoveChild(…) / ReplaceChild(…)
 Removes / replaces given child
13
The XmlNode Class (5)
 RemoveAll() – deletes all children of the
current node (element, attribute …)
 Value, InnerText, InnerXml – changes the
value / text / XML text of the node
14
The XmlDocument Class
 The System.Xml.XmlDocument:
 Contains XML document represented as DOM
tree
 Allows loading and saving XML document (from
files, streams or strings)
 Load(…), LoadXml(…), Save(…)
 Important properties, methods and events:
 DocumentElement – returns the root element
15
The XmlDocument Class (2)
 Important properties, methods and events:
 PreserveWhitespace – indicates if the white
space to be kept during save or load
 CreateElement(…), CreateAttribute(…),
CreateTextNode(…) – creates new
XML element, attribute or value for the
element
 NodeChanged, NodeInserted, NodeRemoved
– events for tracking the changes in the
document
16
Modifying XML Document
Using the DOM Parser
Live Demo
The Streaming Parser
Using XmlReader and XmlWriter
SAX and StAX Parsers
 SAX-style parsers are 'push' parsers
 'Push' data to the application
 Event handlers for processing tags, attributes,
data, whitespace, etc.
 StAX-like parsers are 'pull' parsers
 'Pull' the information from the parser as
needed
 The application moves the document's cursor
forward (like reading a stream)
 XmlReader is StAX-style (streaming) parser
19
SAX and StAX Parsers
and XmlReader
 In .NET stream processing of XML document is
done with the XmlReader class
 XmlReader is abstract class, which:
 Provides read-only access to the XML data
 Works like a stream, but reads XML documents
 Works in forward-only mode
 Read at each step data (tags, attributes, text)
can be extracted and analyzed
20
The XmlReader Class
 The XmlReader class – most important
methods and properties:
 Read() – reads next node from the XML
document or returns false if no such node
 NodeType – returns the type of the read node
 Name – returns the name of the read node
(name of the last read element, attribute, …)
 HasValue – returns true if the node has value
 Value – returns the node value
21
The XmlReader Class (2)
 ReadElementString() – reads the value (the
text) from the last read element
 AttributeCount, GetAttribute(…) –
extract the attributes of the current element
 XmlReader is an abstract class
 Instantiated with the static method Create(…)
 Implements IDisposable
22
using (XmlReader reader = XmlReader.Create("items.xml"))
{
while (reader.Read()) { … }
}
Working with XmlReader
Live Demo
Working with XmlWriter
 The class XmlWriter creates XML documents
 Works as a stream, but writes tags and data
into XML documents
 Most important methods:
 WriteStartDocument() – adds the prolog part
in the beginning of the document (<?xml …)
 WriteStartElement(…) – adds opening tag
 WriteEndElement() – closes the last tag
 WriteElementString(…) – adds an element
by defined name and text value
24
Working with XmlWriter (2)
 WriteAttributeString(…) – adds an
attribute to the current element
 WriteEndDocument() – closes all tags and
flushes the internal buffer (by calling Flush())
 XmlWriter is an abstract class
 Instantiated by that static method Create(…)
 Implements IDisposable
25
using (var writer = new XmlTextWriter ("items.xml"))
{
// Write nodes here
}
Working with XmlWriter
Live Demo
Using XPath
Using XPath in .NET
 XPath functionality is implemented in the
System.Xml.XPath namespace
 XPath can be used directly from the class
XmlNode (and all its inheritors) :
 SelectNodes(string xPathExpression)
 Returns list of all nodes matched by the specified
XPath expression
 SelectSingleNode(string xPathExpression)
 Returns the first node matched by the specified
XPath expression
28
XPath and XmlNode – Example
 Suppose we want to find the beer's nodes
29
<?xml version="1.0" encoding="windows-1251"?>
<items>
<item type="beer">
<name>Zagorka</name>
<price>0.75</price>
</item>
<item type="food">
<name>sausages</name>
<price>0.48</price>
</item>
<item type="beer">
<name>Kamenitza</name>
<price>0.65</price>
</item>
</items>
XPath and XmlNode – Example (2)
30
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("../../items.xml");
string xPathQuery = "/items/item[@type='beer']";
XmlNodeList beersList =
xmlDoc.SelectNodes(xPathQuery);
foreach (XmlNode beerNode in beersList)
{
string beerName =
beerNode.SelectSingleNode("name");
Console.WriteLine(beerName.InnerText);
}
}
Searching with XPath
in XML Documents
Live Demo
LINQ to XML
LINQ to XML
 LINQ to XML
 Use the power of LINQ to process XML data
 Easily read, write, modify, and search in XML
documents
 Elements and attributes are no longer built-in
the context of their parents
 LINQ to XML classes:
 XDocument – represents a LINQ-enabled XML
document (containing prolog, root element, …)
33
LINQ to XML Classes
 LINQ to XML classes:
 XElement – represents an XML element
 The most fundamental class in LINQ to XML
 Important methods: Parse, Save,
RemoveAtributes, SetElementValue,
SetAtributeValue, WriteTo
 XAttribute – name/ value attribute pair
 XName – tag name + optional namespace –
{namespace}localname
 XNamespace – XML namespace
34
LINQ to XML – How Easy It Is!
 Need to create this XML fragment?
35
XElement books =
new XElement("books",
new XElement("book",
new XElement("author", "Don Box"),
new XElement("title", "ASP.NET")
)
);
<books>
<book>
<author>Don Box</author>
<title>Essential .NET</title>
</book>
</book>
Creating Document
with LINQTo XML
Live Demo
LINQ to XML and Namespaces
 Elements in LINQ to XML have fully expanded
names (namespace + name)
 Easy to manipulate the elements in different
namespaces
37
XNamespace ns = "http://linqinaction.net";
XNamespace anotherNS = "http://publishers.org";
XElement book = new XElement(ns + "book",
new XElement(ns + "title", "LINQ in Action"),
new XElement(ns + "author", "Manning"),
new XElement(ns + "author", "Steve Eichert"),
new XElement(ns + "author", "Jim Wooley"),
new XElement(anotherNS + "publisher", "Manning")
);
LINQ to XML and
Namespaces
Live Demo
LINQ to XML –
Searching with LINQ
 Searching in XML with LINQ is as easy as
searching within an array
39
XDocument xmlDoc = XDocument.Load("../../books.xml");
var books =
from book in xmlDoc.Descendants("book")
where book.Element("title").Value.Contains("4.0")
select new {
Title = book.Element("title").Value,
Author = book.Element("author").Value };
foreach (var book in books)
{
Console.WriteLine(book);
}
Searching in XML
with LINQ Queries
Live Demo
XSLTransformations
XSLTransformations
 XSL transformations (XSLT)
 Convert a XML document to another XML
document with different structure
 Can convert XML to any text format
 Can be used to transform XML documents to
XHTML
 XSLT depends on XPath
 For matching sections from the input document
and replacing them with other text
42
Performing XSLT in .NET
 .NET has built-in XSLT engine
 System.Xml.Xsl.XslCompiledTransform
 Methods:
 Load(…)
 Loads XSL shylesheet for transforming XML docs
 Transform(…)
 Performs transformation of given XML
 Output is written to a XML file, stream or
XmlWriter
43
XSLTransformation – Example
 Transforming XML document by given XSL
stylesheet:
44
using System;
using System.Xml.Xsl;
class XSLTransformDemo
{
static void Main()
{
XslCompiledTransform xslt =
new XslCompiledTransform();
xslt.Load("catalog.xsl");
xslt.Transform("catalog.xml", "catalog.html");
}
}
Transforming
XML by XSL
Stylesheet
форумпрограмиране,форум уеб дизайн
курсовеи уроци по програмиране,уеб дизайн – безплатно
програмиранеза деца – безплатни курсове и уроци
безплатенSEO курс -оптимизация за търсачки
уроципо уеб дизайн, HTML,CSS, JavaScript,Photoshop
уроципо програмиранеи уеб дизайн за ученици
ASP.NETMVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC
безплатенкурс"Разработка на софтуер в cloud среда"
BGCoder -онлайн състезателна система -online judge
курсовеи уроци по програмиране,книги – безплатно отНаков
безплатенкурс"Качествен програменкод"
алгоакадемия – състезателно програмиране,състезания
ASP.NETкурс -уеб програмиране,бази данни, C#,.NET,ASP.NET
курсовеи уроци по програмиране– Телерик академия
курсмобилни приложения с iPhone, Android,WP7,PhoneGap
freeC#book, безплатна книга C#,книга Java,книга C#
Дончо Минков -сайт за програмиране
Николай Костов -блог за програмиране
C#курс,програмиране,безплатно
Processing XML in .NET
http://academy.telerik.com/student-courses/software-technologies/databases/
Exercises
1. Create a XML file representing catalogue.The
catalogue should contain albums of different artists.
For each album you should define: name, artist,
year, producer, price and a list of songs. Each
song should be described by title and duration.
2. Write program that extracts all different artists
which are found in the catalog.xml. For each
author you should print the number of albums in the
catalogue. Use the DOM parser and a hash-table.
3. Implement the previous using XPath.
4. Using the DOM parser write a program to delete
from catalog.xml all albums having price > 20.
47
Exercises (2)
5. Write a program, which using XmlReader extracts
all song titles from catalog.xml.
6. Rewrite the same using XDocument and LINQ query.
7. In a text file we are given the name, address and
phone number of given person (each at a single line).
Write a program, which creates new XML document,
which contains these data in structured XML format.
8. Write a program, which (using XmlReader and
XmlWriter) reads the file catalog.xml and creates
the file album.xml, in which stores in appropriate
way the names of all albums and their authors.
48
Exercises (3)
9. Write a program to traverse given directory and
write to a XML file its contents together with all
subdirectories and files. Use tags <file> and <dir>
with appropriate attributes. For the generation of
the XML document use the class XmlWriter.
10. Rewrite the last exercises using XDocument,
XElement and XAttribute.
11. Write a program, which extract from the file
catalog.xml the prices for all albums, published 5
years ago or earlier. Use XPath query.
12. Rewrite the previous using LINQ query.
49
Exercises (4)
13. Create an XSL stylesheet, which transforms the file
catalog.xml into HTML document, formatted for
viewing in a standard Web-browser.
14. Write a C# program to apply the XSLT stylesheet
transformation on the file catalog.xml using the
class XslTransform.
15. * Read some tutorial about the XQuery language.
Implement the XML to HTML transformation with
XQuery (instead of XSLT). Download some open
source XQuery library for .NET and execute the
XQuery to transform the catalog.xml to HTML.
50
Exercises (5)
15. UsingVisual Studio generate an XSD schema for the
file catalog.xml. Write a C# program that takes an
XML file and an XSD file (schema) and validates the
XML file against the schema.Test it with valid XML
catalogs and invalid XML catalogs.
51
Promotion
 Promotion – the beer became free (hurrah !!!)
52
FreeTrainings @Telerik Academy
 "Web Design with HTML 5, CSS 3 and
JavaScript" course @Telerik Academy
 html5course.telerik.com
 Telerik Software Academy
 academy.telerik.com
 Telerik Academy @ Facebook
 facebook.com/TelerikAcademy
 Telerik Software Academy Forums
 forums.academy.telerik.com

More Related Content

What's hot

Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NETAbhi Arya
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesMarco Gralike
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xsltKumar
 
BGOUG 2012 - Design concepts for xml applications that will perform
BGOUG 2012 - Design concepts for xml applications that will performBGOUG 2012 - Design concepts for xml applications that will perform
BGOUG 2012 - Design concepts for xml applications that will performMarco Gralike
 
Understanding C# in .NET
Understanding C# in .NETUnderstanding C# in .NET
Understanding C# in .NETmentorrbuddy
 
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File ServerUKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File ServerMarco Gralike
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingGurpreet singh
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataMarco Gralike
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 
XFILES, The APEX 4 version - The truth is in there
XFILES, The APEX 4 version - The truth is in thereXFILES, The APEX 4 version - The truth is in there
XFILES, The APEX 4 version - The truth is in thereMarco Gralike
 
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Rathod Shukar
 
04 darwino concepts and utility classes
04   darwino concepts and utility classes04   darwino concepts and utility classes
04 darwino concepts and utility classesdarwinodb
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQLRaji Ghawi
 
Oracle Database 11g Release 2 - XMLDB New Features
Oracle Database 11g Release 2 - XMLDB New FeaturesOracle Database 11g Release 2 - XMLDB New Features
Oracle Database 11g Release 2 - XMLDB New FeaturesMarco Gralike
 

What's hot (19)

6 xml parsing
6   xml parsing6   xml parsing
6 xml parsing
 
Sax parser
Sax parserSax parser
Sax parser
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
 
BGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index StrategiesBGOUG 2012 - XML Index Strategies
BGOUG 2012 - XML Index Strategies
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
BGOUG 2012 - Design concepts for xml applications that will perform
BGOUG 2012 - Design concepts for xml applications that will performBGOUG 2012 - Design concepts for xml applications that will perform
BGOUG 2012 - Design concepts for xml applications that will perform
 
Oracle 10g
Oracle 10gOracle 10g
Oracle 10g
 
Understanding C# in .NET
Understanding C# in .NETUnderstanding C# in .NET
Understanding C# in .NET
 
Xml writers
Xml writersXml writers
Xml writers
 
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File ServerUKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
 
XML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured dataXML Amsterdam - Creating structure in unstructured data
XML Amsterdam - Creating structure in unstructured data
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
XFILES, The APEX 4 version - The truth is in there
XFILES, The APEX 4 version - The truth is in thereXFILES, The APEX 4 version - The truth is in there
XFILES, The APEX 4 version - The truth is in there
 
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
 
04 darwino concepts and utility classes
04   darwino concepts and utility classes04   darwino concepts and utility classes
04 darwino concepts and utility classes
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Oracle Database 11g Release 2 - XMLDB New Features
Oracle Database 11g Release 2 - XMLDB New FeaturesOracle Database 11g Release 2 - XMLDB New Features
Oracle Database 11g Release 2 - XMLDB New Features
 
JAXB
JAXBJAXB
JAXB
 

Similar to 06 xml processing-in-.net

Similar to 06 xml processing-in-.net (20)

The xml
The xmlThe xml
The xml
 
Session 5
Session 5Session 5
Session 5
 
Understanding XML DOM
Understanding XML DOMUnderstanding XML DOM
Understanding XML DOM
 
Dom
Dom Dom
Dom
 
Xml session
Xml sessionXml session
Xml session
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
java API for XML DOM
java API for XML DOMjava API for XML DOM
java API for XML DOM
 
04 sm3 xml_xp_07
04 sm3 xml_xp_0704 sm3 xml_xp_07
04 sm3 xml_xp_07
 
Xml and DTD's
Xml and DTD'sXml and DTD's
Xml and DTD's
 
Applied xml programming for microsoft
Applied xml programming for microsoftApplied xml programming for microsoft
Applied xml programming for microsoft
 
XML
XMLXML
XML
 
It seminar-xml serialization
It seminar-xml serializationIt seminar-xml serialization
It seminar-xml serialization
 
It seminar-xml serialization
It seminar-xml serializationIt seminar-xml serialization
It seminar-xml serialization
 
Xml processing-by-asfak
Xml processing-by-asfakXml processing-by-asfak
Xml processing-by-asfak
 
Xml 2
Xml  2 Xml  2
Xml 2
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Tool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAMLTool Development 05 - XML Schema, INI, JSON, YAML
Tool Development 05 - XML Schema, INI, JSON, YAML
 
XSLT 3.0 - new features
XSLT 3.0 - new featuresXSLT 3.0 - new features
XSLT 3.0 - new features
 

More from glubox

05 entity framework
05 entity framework05 entity framework
05 entity frameworkglubox
 
Sql intro
Sql introSql intro
Sql introglubox
 
01 data modeling-and-er-diagrams
01 data modeling-and-er-diagrams01 data modeling-and-er-diagrams
01 data modeling-and-er-diagramsglubox
 
Bs business plan
Bs business planBs business plan
Bs business planglubox
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29glubox
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500glubox
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500glubox
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500glubox
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29glubox
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29glubox
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29glubox
 

More from glubox (11)

05 entity framework
05 entity framework05 entity framework
05 entity framework
 
Sql intro
Sql introSql intro
Sql intro
 
01 data modeling-and-er-diagrams
01 data modeling-and-er-diagrams01 data modeling-and-er-diagrams
01 data modeling-and-er-diagrams
 
Bs business plan
Bs business planBs business plan
Bs business plan
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500
 
Tz Dch500
Tz Dch500Tz Dch500
Tz Dch500
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
 
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 2972032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
72032 1 844 Hvr Hd250f 160f 28b Manu200239 02 29
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

06 xml processing-in-.net

  • 1. Svetlin Nakov Telerik Software Academy http://academy.telerik.com ManagerTechnicalTraining www.nakov.com Processing XML in .NET DOM Parser, Streaming Parser, XPath, LINQ to XML
  • 2. Table of Contents 1. Processing XML documents in .NET  Using the DOM Parser  Using the Streaming Parser 2. Using XPath to Search in XML Documents 3. Using LINQ-To-XML 4. XSLTransformation in .NET 2
  • 3. The DOM Parser Parsing XML Documents as DOMTrees
  • 4. The DOM Parser  The following XML document is given: 4 <?xml version="1.0"?> <library name=".NET Developer's Library"> <book> <title>Professional C# 4.0 and .NET 4</title> <author>Christian Nagel</author> <isbn>978-0-470-50225-9</isbn> </book> <book> <title>Silverlight in Action</title> <author>Pete Brown</author> <isbn>978-1-935182-37-5</isbn> </book> </library>
  • 5. The DOM Parser (2)  This document is represented in the in the memory as a DOM tree in the following way: 5 document ?xml library version 1.0 encoding utf-8 book title author isbn Professional... Chris... 978-0-470... book title author isbn Silverlight... Pete Brown 978-1... name .NET Developer’s... Header part (prolog) Root node
  • 6. The DOM Parser – Example 6 XmlDocument doc = new XmlDocument(); doc.Load("library.xml"); XmlNode rootNode = doc.DocumentElement; Console.WriteLine("Root node: {0}", rootNode.Name); foreach (XmlAttribute atr in rootNode.Attributes) { Console.WriteLine("Attribute: {0}={1}", atr.Name, atr.Value); } foreach (XmlNode node in rootNode.ChildNodes) { Console.WriteLine("nBook title = {0}", node["title"].InnerText); Console.WriteLine("Book author = {0}", node["author"].InnerText); Console.WriteLine("Book isbn = {0}", node["isbn"].InnerText); }
  • 7. Parsing XML Document with the DOM Parser Live Demo
  • 8. Classes to Work with DOM  The DOM parser provides few important classes:  XmlDocument  Represents the DOM tree  Usually contains two elements:  Header part (prolog)  The root element of the XML document  XmlNode  Abstract base class for all nodes in a DOM tree 8
  • 9. Classes to Work with DOM (2)  XmlElement  Represents a XML element  XmlAttribute  Represents an attribute of an XML tag (couple name-value)  XmlAttributeCollection  List of XML attributes attached to an element  XmlNodeList  List of XML DOM nodes 9
  • 10. The XmlNode Class  The class System.Xml.XmlNode:  Fundamental for the DOM processing  Represents a base node  Its inheritor-classes are:  XmlDocument, XmlElement, XmlAttribute, XmlDeclaration, …  Allows navigation in the DOM tree:  ParentNode – returns the parent node (null for the root) 10
  • 11. The XmlNode Class (2)  PreviousSibling / NextSibling – returns the left / right node to the current  FirstChild / LastChild – returns the first / last child of the current node  Item (indexer [] in C#) – returns the child of the current node by its name  Working with the current node:  Name – returns the name of the node (element, attribute …)  Value – gets the node value 11
  • 12. The XmlNode Class (3)  Attributes – returns the node attributes as XmlAttributeCollection  InnerXml, OuterXml – returns the part of the XML containing the current node  Respectively with or without the node itself  InnerText – concatenation of the values of the node and its inheritors  NodeType – returns the node type 12
  • 13. The XmlNode Class (4)  Changing of the current node:  AppendChild(…) / PrependChild(…)  Inserts new child after / before all other children of the current node  InsertBefore(…) / InsertAfter(…)  Inserts new child before / after given inheritor  RemoveChild(…) / ReplaceChild(…)  Removes / replaces given child 13
  • 14. The XmlNode Class (5)  RemoveAll() – deletes all children of the current node (element, attribute …)  Value, InnerText, InnerXml – changes the value / text / XML text of the node 14
  • 15. The XmlDocument Class  The System.Xml.XmlDocument:  Contains XML document represented as DOM tree  Allows loading and saving XML document (from files, streams or strings)  Load(…), LoadXml(…), Save(…)  Important properties, methods and events:  DocumentElement – returns the root element 15
  • 16. The XmlDocument Class (2)  Important properties, methods and events:  PreserveWhitespace – indicates if the white space to be kept during save or load  CreateElement(…), CreateAttribute(…), CreateTextNode(…) – creates new XML element, attribute or value for the element  NodeChanged, NodeInserted, NodeRemoved – events for tracking the changes in the document 16
  • 17. Modifying XML Document Using the DOM Parser Live Demo
  • 18. The Streaming Parser Using XmlReader and XmlWriter
  • 19. SAX and StAX Parsers  SAX-style parsers are 'push' parsers  'Push' data to the application  Event handlers for processing tags, attributes, data, whitespace, etc.  StAX-like parsers are 'pull' parsers  'Pull' the information from the parser as needed  The application moves the document's cursor forward (like reading a stream)  XmlReader is StAX-style (streaming) parser 19
  • 20. SAX and StAX Parsers and XmlReader  In .NET stream processing of XML document is done with the XmlReader class  XmlReader is abstract class, which:  Provides read-only access to the XML data  Works like a stream, but reads XML documents  Works in forward-only mode  Read at each step data (tags, attributes, text) can be extracted and analyzed 20
  • 21. The XmlReader Class  The XmlReader class – most important methods and properties:  Read() – reads next node from the XML document or returns false if no such node  NodeType – returns the type of the read node  Name – returns the name of the read node (name of the last read element, attribute, …)  HasValue – returns true if the node has value  Value – returns the node value 21
  • 22. The XmlReader Class (2)  ReadElementString() – reads the value (the text) from the last read element  AttributeCount, GetAttribute(…) – extract the attributes of the current element  XmlReader is an abstract class  Instantiated with the static method Create(…)  Implements IDisposable 22 using (XmlReader reader = XmlReader.Create("items.xml")) { while (reader.Read()) { … } }
  • 24. Working with XmlWriter  The class XmlWriter creates XML documents  Works as a stream, but writes tags and data into XML documents  Most important methods:  WriteStartDocument() – adds the prolog part in the beginning of the document (<?xml …)  WriteStartElement(…) – adds opening tag  WriteEndElement() – closes the last tag  WriteElementString(…) – adds an element by defined name and text value 24
  • 25. Working with XmlWriter (2)  WriteAttributeString(…) – adds an attribute to the current element  WriteEndDocument() – closes all tags and flushes the internal buffer (by calling Flush())  XmlWriter is an abstract class  Instantiated by that static method Create(…)  Implements IDisposable 25 using (var writer = new XmlTextWriter ("items.xml")) { // Write nodes here }
  • 28. Using XPath in .NET  XPath functionality is implemented in the System.Xml.XPath namespace  XPath can be used directly from the class XmlNode (and all its inheritors) :  SelectNodes(string xPathExpression)  Returns list of all nodes matched by the specified XPath expression  SelectSingleNode(string xPathExpression)  Returns the first node matched by the specified XPath expression 28
  • 29. XPath and XmlNode – Example  Suppose we want to find the beer's nodes 29 <?xml version="1.0" encoding="windows-1251"?> <items> <item type="beer"> <name>Zagorka</name> <price>0.75</price> </item> <item type="food"> <name>sausages</name> <price>0.48</price> </item> <item type="beer"> <name>Kamenitza</name> <price>0.65</price> </item> </items>
  • 30. XPath and XmlNode – Example (2) 30 static void Main() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("../../items.xml"); string xPathQuery = "/items/item[@type='beer']"; XmlNodeList beersList = xmlDoc.SelectNodes(xPathQuery); foreach (XmlNode beerNode in beersList) { string beerName = beerNode.SelectSingleNode("name"); Console.WriteLine(beerName.InnerText); } }
  • 31. Searching with XPath in XML Documents Live Demo
  • 33. LINQ to XML  LINQ to XML  Use the power of LINQ to process XML data  Easily read, write, modify, and search in XML documents  Elements and attributes are no longer built-in the context of their parents  LINQ to XML classes:  XDocument – represents a LINQ-enabled XML document (containing prolog, root element, …) 33
  • 34. LINQ to XML Classes  LINQ to XML classes:  XElement – represents an XML element  The most fundamental class in LINQ to XML  Important methods: Parse, Save, RemoveAtributes, SetElementValue, SetAtributeValue, WriteTo  XAttribute – name/ value attribute pair  XName – tag name + optional namespace – {namespace}localname  XNamespace – XML namespace 34
  • 35. LINQ to XML – How Easy It Is!  Need to create this XML fragment? 35 XElement books = new XElement("books", new XElement("book", new XElement("author", "Don Box"), new XElement("title", "ASP.NET") ) ); <books> <book> <author>Don Box</author> <title>Essential .NET</title> </book> </book>
  • 37. LINQ to XML and Namespaces  Elements in LINQ to XML have fully expanded names (namespace + name)  Easy to manipulate the elements in different namespaces 37 XNamespace ns = "http://linqinaction.net"; XNamespace anotherNS = "http://publishers.org"; XElement book = new XElement(ns + "book", new XElement(ns + "title", "LINQ in Action"), new XElement(ns + "author", "Manning"), new XElement(ns + "author", "Steve Eichert"), new XElement(ns + "author", "Jim Wooley"), new XElement(anotherNS + "publisher", "Manning") );
  • 38. LINQ to XML and Namespaces Live Demo
  • 39. LINQ to XML – Searching with LINQ  Searching in XML with LINQ is as easy as searching within an array 39 XDocument xmlDoc = XDocument.Load("../../books.xml"); var books = from book in xmlDoc.Descendants("book") where book.Element("title").Value.Contains("4.0") select new { Title = book.Element("title").Value, Author = book.Element("author").Value }; foreach (var book in books) { Console.WriteLine(book); }
  • 40. Searching in XML with LINQ Queries Live Demo
  • 42. XSLTransformations  XSL transformations (XSLT)  Convert a XML document to another XML document with different structure  Can convert XML to any text format  Can be used to transform XML documents to XHTML  XSLT depends on XPath  For matching sections from the input document and replacing them with other text 42
  • 43. Performing XSLT in .NET  .NET has built-in XSLT engine  System.Xml.Xsl.XslCompiledTransform  Methods:  Load(…)  Loads XSL shylesheet for transforming XML docs  Transform(…)  Performs transformation of given XML  Output is written to a XML file, stream or XmlWriter 43
  • 44. XSLTransformation – Example  Transforming XML document by given XSL stylesheet: 44 using System; using System.Xml.Xsl; class XSLTransformDemo { static void Main() { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("catalog.xsl"); xslt.Transform("catalog.xml", "catalog.html"); } }
  • 46. форумпрограмиране,форум уеб дизайн курсовеи уроци по програмиране,уеб дизайн – безплатно програмиранеза деца – безплатни курсове и уроци безплатенSEO курс -оптимизация за търсачки уроципо уеб дизайн, HTML,CSS, JavaScript,Photoshop уроципо програмиранеи уеб дизайн за ученици ASP.NETMVCкурс – HTML,SQL,C#,.NET,ASP.NETMVC безплатенкурс"Разработка на софтуер в cloud среда" BGCoder -онлайн състезателна система -online judge курсовеи уроци по програмиране,книги – безплатно отНаков безплатенкурс"Качествен програменкод" алгоакадемия – състезателно програмиране,състезания ASP.NETкурс -уеб програмиране,бази данни, C#,.NET,ASP.NET курсовеи уроци по програмиране– Телерик академия курсмобилни приложения с iPhone, Android,WP7,PhoneGap freeC#book, безплатна книга C#,книга Java,книга C# Дончо Минков -сайт за програмиране Николай Костов -блог за програмиране C#курс,програмиране,безплатно Processing XML in .NET http://academy.telerik.com/student-courses/software-technologies/databases/
  • 47. Exercises 1. Create a XML file representing catalogue.The catalogue should contain albums of different artists. For each album you should define: name, artist, year, producer, price and a list of songs. Each song should be described by title and duration. 2. Write program that extracts all different artists which are found in the catalog.xml. For each author you should print the number of albums in the catalogue. Use the DOM parser and a hash-table. 3. Implement the previous using XPath. 4. Using the DOM parser write a program to delete from catalog.xml all albums having price > 20. 47
  • 48. Exercises (2) 5. Write a program, which using XmlReader extracts all song titles from catalog.xml. 6. Rewrite the same using XDocument and LINQ query. 7. In a text file we are given the name, address and phone number of given person (each at a single line). Write a program, which creates new XML document, which contains these data in structured XML format. 8. Write a program, which (using XmlReader and XmlWriter) reads the file catalog.xml and creates the file album.xml, in which stores in appropriate way the names of all albums and their authors. 48
  • 49. Exercises (3) 9. Write a program to traverse given directory and write to a XML file its contents together with all subdirectories and files. Use tags <file> and <dir> with appropriate attributes. For the generation of the XML document use the class XmlWriter. 10. Rewrite the last exercises using XDocument, XElement and XAttribute. 11. Write a program, which extract from the file catalog.xml the prices for all albums, published 5 years ago or earlier. Use XPath query. 12. Rewrite the previous using LINQ query. 49
  • 50. Exercises (4) 13. Create an XSL stylesheet, which transforms the file catalog.xml into HTML document, formatted for viewing in a standard Web-browser. 14. Write a C# program to apply the XSLT stylesheet transformation on the file catalog.xml using the class XslTransform. 15. * Read some tutorial about the XQuery language. Implement the XML to HTML transformation with XQuery (instead of XSLT). Download some open source XQuery library for .NET and execute the XQuery to transform the catalog.xml to HTML. 50
  • 51. Exercises (5) 15. UsingVisual Studio generate an XSD schema for the file catalog.xml. Write a C# program that takes an XML file and an XSD file (schema) and validates the XML file against the schema.Test it with valid XML catalogs and invalid XML catalogs. 51
  • 52. Promotion  Promotion – the beer became free (hurrah !!!) 52
  • 53. FreeTrainings @Telerik Academy  "Web Design with HTML 5, CSS 3 and JavaScript" course @Telerik Academy  html5course.telerik.com  Telerik Software Academy  academy.telerik.com  Telerik Academy @ Facebook  facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com