+ All Categories
Home > Documents > Presentation

Presentation

Date post: 06-Jan-2016
Category:
Upload: keenan
View: 17 times
Download: 0 times
Share this document with a friend
Description:
Presentation. XML . NET SEMINAR. By: Siddhant Ahuja (SID). Outline. XML.NET Seminar by Sid. 1. Introduction to XML Mark-up Languages XML Evolution and W3C 2. XML and Microsoft .NET 3. XML Parsing Models Common Parsing Models DOM, Forward only Push (SAX)/Pull Model Parsing, - PowerPoint PPT Presentation
39
Presentati on XML . NET SEMINAR By: Siddhant Ahuja (SID)
Transcript
Page 1: Presentation

Presentation

XML . NET SEMINARBy:

Siddhant Ahuja (SID)

Page 2: Presentation

XML.NET Seminar by SidOutline

1. Introduction to XML Mark-up Languages

XML Evolution and W3C

2. XML and Microsoft .NET

3. XML Parsing Models

Common Parsing Models

DOM, Forward only Push (SAX)/Pull Model Parsing,

.NET Framework Parsing Models

Forward only Pull Model Parsing

DOM Parsing

Advantages and Limitations of each

4. Core XML.NET Namespaces

Page 3: Presentation

XML.NET Seminar by SidOutline

5. Dynamically generating XML in .NET Non-cached, forward only streaming

Programming the DOM

Advantages and Limitations of each

6. Validating XML in .NET DTD, XDR, XSD

Schemas in .NET

7. Applying Style sheets to XML Documents XSLT in .NET

BREAK: Q/ABREAK: Q/A

Page 4: Presentation

XML.NET Seminar by SidOutline8. XPath Queries

9. Summary and Q/A

Page 5: Presentation

XML.NET Seminar by Sid

XML is Born

XML and Microsoft. NET

• Subset of SGML• XML is a mark up that describes structure • Acts as an integration protocol between applications• Industry standard developed by W3C

Page 6: Presentation

XML.NET Seminar by Sid

Introduction to XML

Intro

Mark up is:

Adding Meaning to the Text

Changing format of the Text

Example:

<Presenter> Sid </Presenter>

<Bold> Sid </Bold>

Page 7: Presentation

XML.NET Seminar by Sid

Making an XML file

Intro

<?xml version="1.0" encoding="UTF-8"?>

<PRODUCTDATA>

<PRODUCT PRODID="P001"><PRODUCTNAME>Windows XP SP2</PRODUCTNAME><DESCRIPTION>This is the latest update pack provided by

Microsoft.</DESCRIPTION><DETAILS>Has Firewall Protection</DETAILS><PRICE>Free</PRICE><SIZE>~200 MB for Windows XP Home Edition</SIZE>

</PRODUCT>

</PRODUCTDATA>

Page 8: Presentation

XML.NET Seminar by Sid

XML on the Web

Intro

Page 9: Presentation

<?xml version="1.0" encoding="UTF-8"?>

<PRODUCTDATA>

<PRODUCT PRODID="P001"><PRODUCTNAME>Windows XP SP2</PRODUCTNAME><DESCRIPTION>This is the latest update pack provided by

Microsoft.</DESCRIPTION><DETAILS>Has Firewall Protection</DETAILS><PRICE>Free</PRICE><SIZE>~200 MB for Windows XP Home Edition</SIZE>

</PRODUCT>

</PRODUCTDATA>

Root ElementRoot Element

XML.NET Seminar by Sid

XML Structure

Intro

Parent ElementParent ElementChild Child

ElementElement

AttributeAttribute

ContentContent

DeclarationDeclaration

Page 10: Presentation

XML.NET Seminar by Sid

.NET Framework

Base Class LibraryBase Class Library

Common Language SpecificationCommon Language Specification

Common Language RuntimeCommon Language Runtime

ADO .NET: Data and XMLADO .NET: Data and XML

VBVB C++C++ C#C#V

isual S

tud

io .N

ET

Visu

al Stu

dio

.NE

T

ASP .NET: Web ServicesASP .NET: Web Servicesand Web Formsand Web Forms

JScriptJScript ……

WindowsWindowsFormsForms

Page 11: Presentation

XML.NET Seminar by Sid

XML and ADO.NET (unified architecture)

Sync

DataReader

SqlData-Reader

OleDbData-Reader

Controls,Designers,

Code-gen, etc.

DataSet

XmlReader

XmlText-Reader

XmlNode-Reader

XSL/T, X-Path,Validation, etc.

XmlData-Document

DataAdapter

SqlData-Adapter

OleDbData-Adapter

Page 12: Presentation

XML.NET Seminar by Sid

XML and .NET

XML and .NET

• XML: Industry standard; interoperability mechanism between applications

• .NET: Microsoft vision of the future of distributed applications

• XML is a glue that holds all .NET components together

Page 13: Presentation

XML.NET Seminar by Sid

XML.NET and Web Services

XML and .NET

ClientClient

WebService

WebService

WebService

WebService

WebService

WebService Web

ServiceWeb

Service

ClientClientXMLXML

XMLXML XMLXML

XMLXML

XMLXML

XMLXML

HTMLHTML

Page 14: Presentation

XML.NET Seminar by Sid

XML Parsing Models

XML Parsing Models

• Commonly known XML parsing models :– The DOM model– Forward only push model parsing (SAX)– Forward only pull model parsing

• The .NET Framework XML parsing models– Forward only pull model parsing– DOM Parsing

• Advantages and limitations of each model

Page 15: Presentation

XML.NET Seminar by Sid

DOM Model

XML Parsing Models

• In memory XML parsing– A tree structure is created in memory to represent the

contents of the XML document being parsed• The parsing model of choice when there is a need to

dynamically navigate through and manipulate (insert, update, and delete) the contents of an XML document

• Not a good choice when the only requirement is to parse an XML document from top to bottom in a read-only fashion

• Memory intensive – loading very large XML documents into the DOM can deplete system resources

Page 16: Presentation

XML.NET Seminar by Sid

DOM Parsing

XML Parsing Models

<?xml version="1.0"?><Books> <Book ISBN="0355605172"/> <Title>Beginning XML</Title> <Price>40.00</Price> </Book> <Book ISBN="0415205173"/> <Title>XML Step by Step</Title> <Price>50.00</Price> </Book></Books>

Page 17: Presentation

XML.NET Seminar by Sid

DOM Parsing- Accessing and Modifying Element Data

XML Parsing Models

Dim xmldoc As New XmlDocument()xmldoc.Load("c:\books.xml")

Dim PriceNodes As XmlNodeListDim PriceNode As XmlNode

Dim Price As Double

PriceNodes = xmldoc.GetElementsByTagName("Price") For Each PriceNode In PriceNodes Price = CType(PriceNode.InnerText, Double) If Price >= 50 Then Price = Price - ((5 / 100) * Price) PriceNode.InnerText = CType(Price, String) End IfNext

xmldoc.Save("c:\books.xml")

Page 18: Presentation

XML.NET Seminar by Sid

Core XML.NET Namespaces

Core XML.NET Namespaces

• System.Xml– The overall namespace for the .NET Framework

classes provide standards-based support for parsing XML

– Supported W3C XML standards:• XML 1.0 and XML namespaces• XML schemas• XPath• XSLT• DOM level 2 core• SOAP 1.1 (used in object serialization)

Page 19: Presentation

XML.NET Seminar by Sid

Core XML.NET Namespaces

Core XML.NET Namespaces

• System.Xml.Xsl

– Contains classes that provide support for XSLT transformations

• System.Xml.XPath

– Contains classes that provide support for executing XPath queries

• System.Xml.Schema

– Contains classes that provide standards-based support for W3C XML schemas

• System.Xml.Serialization

– Contains classes that are used to serialize and de-serialize .NET Framework objects to and from XML

Page 20: Presentation

XML.NET Seminar by Sid

Dynamically generating XML in .NET

Dynamically generating XML in .NET

• Options available to programmatically generate XML: • Non-cached, forward-only streaming• Programming the DOM

• Advantages and limitations of each method

Page 21: Presentation

XML.NET Seminar by Sid

Using XmlTextWriter Class

Dynamically generating XML in .NET

• Implemented in the System.Xml .NET Framework namespace

• Inherits from the System.Xml.XmlWriter abstract class

• Used to programmatically generate XML in a non-cached, forward-only fashion

• Can be used to generate XML to a file on disk and .NET Framework Stream/TextWriter objects

Page 22: Presentation

XML.NET Seminar by Sid

Using XmlTextWriter Class

Dynamically generating XML in .NET

<?xml version="1.0"?>

<!--Catalog fragment-->

<!DOCTYPE Books SYSTEM "books.dtd">

<Books>

<Book ISBN="0355605172"/>

<Title>XML Step by Step</Title>

</Book>

</Books>

Page 23: Presentation

XML.NET Seminar by Sid

Dim wrt As XmlTextWriter = New XmlTextWriter("c:\books.xml", Nothing) wrt.Formatting = System.Xml.Formatting.Indented

wrt.WriteStartDocument(False) wrt.WriteComment("Catalog fragment") wrt.WriteDocType("Books", Nothing, "books.dtd", Nothing)

wrt.WriteStartElement("Books")

wrt.WriteStartElement("Book") wrt.WriteAttributeString("", "ISBN", "", "0355605172") wrt.WriteStartElement("Title") wrt.WriteString(“XML Step by Step") wrt.WriteEndElement()

wrt.WriteEndElement()

wrt.WriteEndElement()

wrt.Close()

Using XmlTextWriter Class

Page 24: Presentation

XML.NET Seminar by Sid

Using DOM

Dim xmldoc As New XmlDocument()Dim xmldecl As XmlDeclarationDim xmlComment As XmlCommentDim docType As XmlDocumentTypeDim xmlfragment As XmlDocumentFragment

xmldecl = xmldoc.CreateXmlDeclaration("1.0", Nothing, Nothing)xmldoc.AppendChild(xmldecl)

docType = xmldoc.CreateDocumentType("Books", Nothing, "c:\books.dtd", Nothing)xmldoc.AppendChild(docType)

xmlComment = xmldoc.CreateComment("Catalog fragment")xmldoc.AppendChild(xmlComment) xmldoc.AppendChild(xmldoc.CreateElement("Books"))

xmldoc.DocumentElement.AppendChild(GenerateBookNode(xmldoc, "XML Step by Step", "0355605172"))

xmldoc.Save("c:\books2.xml")

Page 25: Presentation

XML.NET Seminar by Sid

Using DOM

Private Function GenerateBookNode(ByVal xmldoc As XmlDocument, ByVal Title As String, ByVal ISBN As String) As XmlNode

Dim BookNode As XmlNode

BookNode = xmldoc.CreateElement("Book") BookNode.AppendChild(xmldoc.CreateElement("Title")) BookNode.ChildNodes(0).InnerText = Title

BookNode.Attributes.Append(xmldoc.CreateAttribute("ISBN")) BookNode.Attributes.GetNamedItem("ISBN").InnerText = ISBN

GenerateBookNode = BookNode

End Function

Page 26: Presentation
Page 27: Presentation

XML.NET Seminar by Sid

Validation of XML

Validating XML in .NET

• Schemas help define the structure of XML documents. Validation ensures that the external data conforms to the rules (grammar) required by the schema.

• The three language recommendations: – Document Type Definitions (DTD)– XML Data Reduced schema (XDR)– XML Schema Definition language (XSD)

• XSD is the future. Schemas have several advantages over DTD:– Schemas use XML syntax and can be parsed by an XML parser.– Schemas offer data type support (integer, string, Boolean), and the

ability to create other data types.

Page 28: Presentation

XML.NET Seminar by Sid

Schemas in .NET

Validating XML in .NET

• XML data can be validated against all the three schema languages using the .NET classes.

• System.Xml.XmlValidatingReader is used for validation.• System.Xml.Schema is the namespace for the XML

classes that provide standards-based support for XML schemas (for structures and data types).

• System.Xml.Schema.XmlSchemaCollection Class contains a cache of XSD and XDR schemas.

Page 29: Presentation

XML.NET Seminar by Sid

Code Sample

Public Shared Sub Main() ' Add the schema to a schemaCollection instance Dim sc As XmlSchemaCollection = New XmlSchemaCollection() sc.Add(Nothing, "schema.xsd")

If (sc.Count > 0) Then ' Initialize the validating reader Dim tr As XmlTextReader = New XmlTextReader("booksSchemaFail.xml") Dim rdr As XmlValidatingReader = New XmlValidatingReader(tr)

' Set the validation type to XSD Schema rdr.ValidationType = ValidationType.Schema rdr.Schemas.Add(sc)

' Add a validation event handler and read the data AddHandler rdr.ValidationEventHandler, AddressOf ValidationCallBack While (rdr.Read()) End While End If

End Sub

Public Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs) Console.WriteLine("XSD Error: {0}", e.Message)End Sub

Page 30: Presentation

XML.NET Seminar by Sid

XSLT Transformations in .NET

Applying Style-sheets to XML Documents

• XSL (Extensible Stylesheet Language) consists of three parts:• XSLT – XSL transformations• XPath – XML path language• XSL-FO – XSL formatting objects

• XSLT is a language for transforming XML documents into text-based documents

• Transformation process involves three documents:• Source XML document• Stylesheet document• Output document – XML, HTML, etc.

Page 31: Presentation

XML.NET Seminar by Sid

XSLT in .NET

Applying Style-sheets to XML Documents

• Implemented under System.Xml.Xsl namespace• Supports W3C XSLT 1.0 recommendation• Key classes:

• XslTransform – Transforms XML data using an XSLT stylesheet

• XsltArgumentList – Allows parameters and extension objects to be invoked from within the stylesheet

• XsltException – Returns information about the last exception thrown while processing an XSL transform

Page 32: Presentation

XML.NET Seminar by Sid

XslTransform

‘ Transforms the XML data in the input file and outputs the result to an output fileDim xslt As XslTransform = New XslTransform() xslt.Load("books.xsl") xslt.Transform("books.xml", "output.xml")

' Transforms the XML data in the XPathDocument and outputs the result to an XmlReaderDim URL As String =

"http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/VB/"Dim xslt As XslTransform = New XslTransform()xslt.Load(URL & "books.xsl")

Dim doc As XPathDocument = New XPathDocument(URL & "books.xml")Dim reader As XmlReader = xslt.Transform(doc, Nothing)

‘ Transforms the XML data in the input file and outputs the result to a streamDim xslt As XslTransform = New XslTransform()xslt.Load("books.xsl")

Dim strm As New MemoryStream()xslt.Transform(New XPathDocument("books.xml"), Nothing, strm)

Page 33: Presentation

XML.NET Seminar by Sid

Overview of XPath

XPath Queries

• A query language for XML – the SQL of XML• Used to specify query expressions to locate nodes in an

XML document• Used in XSLT stylesheets to locate and apply

transformations to specific nodes in an XML document• Used in DOM code to locate and process specific nodes in

an XML document

Page 34: Presentation

XML.NET Seminar by Sid

XPath in .NET

• Related namespaces• System.Xml• System.Xml.XPath

• Key .NET Framework classes• XmlDocument, XmlNodeList, and XmlNode• XPathDocument• XPathNavigator• XPathNodeIterator• XPathExpression

XPath Queries

Page 35: Presentation

XML.NET Seminar by Sid

Executing XPath Queries

Using the System.Xml DOM Objects

• Commonly used classes: XMLDocument, XMLNodeList, XMLNode• Load the XML document into the XMLDocument class• Use the selectNodes/selectSingleNode method of the XmlDocument

class to execute the XPath query• Assign returned node list/node to an XmlNodeList/XmlNode object• Use an XmlNode object to iterate through the XmlNodeList and

process the results

XPath Queries

Page 36: Presentation

XML.NET Seminar by Sid

Executing XPath Queries

• Sample XML document:

<?xml version="1.0"?>

<Books>

<Book ISBN="0355605172"/>

<Title>The C Programming language</Title>

</Book>

<Book ISBN="0735605173"/>

<Title>XML Step by Step</Title>

</Book>

</Books>• Sample XPath query:

– Select all titles that begin with the word ‘XML’• XPath Query : //Title[starts-with(.,’XML’)]

Page 37: Presentation

XML.NET Seminar by Sid

Executing XPath Queries

Dim xmlDoc as New XmlDocument

Dim matchingNodes as XmlNodeList

Dim matchingNode as XmlNode

xmlDoc.Load “c:\books.xml”

matchingNodes = xmlDoc.SelectNodes(“//Title[starts-with(.,’XML’)]”)

If matchingNodes.Count = 0 Then

MsgBox("No matching nodes were identified for the specified XPath query“)

Else

For Each matchingNode In matchingNodes

MsgBox(matchingNode.Name & " : " & matchingNode.InnerText)

Next

End If

Page 38: Presentation

XML.NET Seminar by Sid

Summary

Summary

• Introduced XML in .NET• Introduced the XML parsing models in the .NET Framework• Examined DOM parsing in the .NET Framework• Introduced the XML related .NET Framework namespaces • Examined the process of programmatically generating XML in

the .NET Framework• Examined the process of programmatically validating XML

documents• Executing XSLT transformations• Executing XPath queries

Page 39: Presentation

Recommended