XML and XPath details

38
XML DOM Functionality in .NET DSK Chakravarthy http://dskc.blogspot.com [email protected] http://msmvps.com/blogs/Chakravarthy

description

This is the presentation that I have done for Pre-TechEd of 2006

Transcript of XML and XPath details

Page 1: XML and XPath details

XML DOM Functionality in .NET

DSK Chakravarthyhttp://dskc.blogspot.com

[email protected]

http://msmvps.com/blogs/Chakravarthy

Page 2: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Agenda

• You can see the agenda along the entire presentation

• Q&A is at the End of session

Page 3: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

What I talk about

• The basics of reading and writing XML, programmatically

• The important classes associated with XML documents in the System.XML namespace

• How to Read XML Data• How to Traverse in XML DOM

using xPath expressions

Page 4: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Are you ready ?

Prerequisites•Basic Understanding of XML•Ability to read code

Page 5: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMGetting Started

• Many apps need to read/write XML data

• Perhaps need to:– Store or retrieve configuration settings– Read/write data stored in XML format– Handle inter-application data transfer

• XML programming required for all these

• System.Xml.XmlDocument class provides support for handling XML

Page 6: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMWhat I’ll cover now

• NET Framework uses XML in many ways – Provides several techniques for working with

XML

• Only cover tree-based, in-memory handling

• Other sessions cover other techniques, including the XmlReader/XmlWriter classes

Page 7: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOM.NET APIs for working with XML

• Two main .NET APIs for working with XML: – Cached, read/write, random access

• XML Document Object Model (DOM)• Tree-based, in-memory cache• Represented by the XmlDocument class

– Non-cached, forward-only, read-only access

• Represented by the XmlReader/XmlWriter classes

Page 8: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMTree-Based XML Handling 1/4

• Using this API, XML data loaded synchronously into a “tree”– Contains root node, and multiple child

nodes– Each XML element corresponds to

one or more nodes in the tree– Each node (except root node) has a

parent node– Each node (except root node) can

have multiple siblings– Each node can have multiple children

Page 9: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMTree-Based XML Handling 2/4

• Data loaded into memory– Provides random access to individual

nodes– Can search for any node, at will

• Tree-based structure represents XML infoset– This is the data stored in the

serialized XML format– API doesn’t care about angle brackets

and quotes • Just the data and its relationships

Page 10: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMTree-Based XML Handling 3/4

• Turns this XML content:Turns this XML content:<bookstore> <book genre="novel" publicationdate="1997" ISBN="1-861001-57-8"> <title>Pride And Prejudice</title> <author> <first-name>Jane</first-name> <last-name>Austen</last-name> </author> <price>24.95</price> </book> </bookstore>

Page 11: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMTree-Based XML Handling 4/4

• Into this data structureInto this data structure

bookstore

book

publication datepublication date

publication datetitle

author

price

firstname

lastname

Page 12: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMNon-Cached XML Handling

• Allows for fast access– No waiting for data to load

• Requires less overhead• Doesn’t allow random access to

data– Data flows to application in the order it

appears within the original XML stream

• Not covered here

Page 13: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMWhat is XML DOM?

• To provide standard mechanism for programmatically working with data– W3C proposed a standard API for working

with XML– XML Document Object Model is the result

• Provides software vendors with API for manipulating XML data

• Before .NET, VB developers used MSXML COM component– Now use System.Xml namespace

Page 14: XML and XPath details

Demonstaration -- 0

investigating a sample file – Grocery store

Page 15: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Introduction to XML DOMSample file as Datastructure

Grocery Store

Name = “Breads”

Store name

#textBrain’s Groceries

department

Name = “Fruits”

department

departments

Item

Id=“B1”Name

#textWonder Bread

Price

#text1.29

New

Item

Id=“B2”

Name

#textBlueberry Muffin

Price

#text3.99

New

Type = “Muffin” Item

Id=“F1”

Name

#textApple

Price

#text0.99

Page 16: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Reading XML DataLoad XML Data

• Need to load XML data into an XmlDocument object– Parse persisted XML into tree-based

structure in memory• Create instance of XmlDocument object• Call Load method, providing file name

– Other alternative means for loading, as well• Work with data in tree-based format• Retrieve OuterXml property to view XML

Page 17: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Reading XML DataWorking with Node’s children 1/2

• Calling Load method fills data structure– Single root element, possibly other nodes

• Sample root node contains three children– XML declaration (processing instruction)– Comment– GroceryStore document element

• XmlDocument inherits from XmlNode class– Document == root node

• Use HasChildNodes and ChildNodes

Page 18: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Reading XML DataWorking with Node’s children 2/2

• Interesting issues:– No recursion

• Just displays information about the immediate children of selected node

– Name properties differ • xml, #comment, #text, real names

– Checking HasChildNodes is redundant

• If your intent is to iterate through the nodes

Page 19: XML and XPath details

Demonstaration -- 1

Load XML DataChild Nodes

Visit All NodesElements By Name

Text Nodes

Page 20: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with NodesRetrieve nodes matching Criteria

• GetElementsByTagName works well, but limited

• For more general criteria, use SelectNodes or SelectSingleNode methods of XmlNode

• Can pass XPath query – XPath is powerful, and complex – Can only touch on it here

Page 21: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with NodesGetting started with xPath 1/2

• xPath expressions "know" where to start looking, based on object whose method you call

• Use "//" separator to indicate what follows is a descendant, at any level

• Use "/" separator to indicate what follows is a direct descendant

Page 22: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with NodesGetting started with xPath 2/2

• For example: – //Department//Name

• Looks within Department node (at any lower level) for Name (at any level below Department)

• Use "*" to look for any element name

• To match root node of starting object, simply list the name

Page 23: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with nodesMore about XPATH 1/2

• To match existence of a child node, – use: //Item[New]/Name

• Looks for Item that contains a New element, then matches Name element of the Item

• To match existence of an attribute, use:– //Item[@Type]

• Looks for a Department node with a Name attribute

Page 24: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with nodesMore about XPATH 2/2

• To match specific values, use: – //Department[@Name= 'Fruits']

• Once you have the XPath expression, and the retrieved node list, iterate through nodes

Page 25: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with nodesReference to specific Node

• Use XmlNode.SelectSingleNode method

• Retrieves reference to first matching node

• Best when:– You know there 's only one match– You actually want a relative (parent?)

of the node you find

Page 26: XML and XPath details

Demonstaration -- 2

Select NodesSelect Single Node

Find Relatives

Page 27: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Working with nodesRetrieve Node attributes

• XmlNode.Attributes property retrieves XmlAttributeCollection object

• Collection of XmlAttribute objects– Nodes with a name, and a value

• If you just want a single attribute– Use GetNamedItem method of Attributes

collection

• Display all department names?– Name is an attribute of Department element

Page 28: XML and XPath details

Demonstaration -- 3

Retrieve Attributes Single Attribute

Page 29: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

XmlDocumentConclusion

• Derived from the XmlNode class• Represents an entire (in memory) XML

document• Supports DOM Level 1 and Level 2 Core

functionality• Reading & writing built on top of XmlReader

& XmlWriter• Load a document and generate the DOM

– Using: URI, file, XmlReader, XmlTextReader or Stream

Page 30: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Summary

• In this session, we looked into:– How to load XML data into memory,

using the XmlDocument class– How to read through XML data

• retrieving elements by name • by using XPATH• by relative path

– How to deal with node attributes

Page 31: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

Questions ?

• Request you to drop me a line

• Doesn’t mean that you forget to post them at B.NET

• You can expect reply from both mail & at the discussion thread

Page 32: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

The Webcast Quiz!

• Four questions, multiple choice type– More than one maybe correct

• Two lucky winners will win an Orchid Music Player

• If you participate in the contest and are among the 2 lucky winners, your name will be featured on http://www.microsoft.com/india/webcasts/

Page 33: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

1. What are the key themes in the Data Platform track at TechEd 2006 India?

A) Programming, Database Management and BI

B) Service Oriented Architecture and Robust Data Management

C) Integrated Solution Scenarios

D) Future of Computing and role of real-world Data Management Intelligence

Page 34: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

2. Select 3 core themes under Data Management at Tech.Ed 2006 India.

A) Security B) Monitoring and Analysis

C) Scalability D) Robust integration of Development Tools and RDBMS

Page 35: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

3. What is the association between Tech.Ed-India 2006 and Vineet Gupta?

A) Track Owner B) Speaker

C) Event Owner D) Keynote Speaker

Page 36: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

4. What is SOA?

A) Service Oriented Architecture

B) Solutions Oriented Architecture

C) Security Oriented Architecture

D) Scalable Operational Architecture

Page 37: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

How to Participate

• Please send your responses to [email protected]– Subject = “Microsoft Webcast

Contest – TechEd 2006 – Data Platform and Architecture”

Page 38: XML and XPath details

Intro to XML DOM

Reading XML Data

Working with Nodes

xPath Expressions

Q & A

Contact ME

© 2006 Microsoft Corporation. All rights reserved.© 2006 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.