C# The new language for Updated by Pavel Ježek © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.

Slides:



Advertisements
Similar presentations
The Document Object Model
Advertisements

XML & Data Structures for the Internet Yingcai Xiao.
XPath Eugenia Fernandez IUPUI. XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing.
Document Object Model. Lecture 18 The Document Object Model (DOM) is not a programming language It is an object-oriented model of web documents Each.
Document Object Model (DOM): An Abstract Data Structure for XML data Alex Dekhtyar Department of Computer Science University of Kentucky.
11-Jun-15 More DOM. Manipulating DOM trees DOM, unlike SAX, gives you the ability to create and modify XML trees There are a few roadblocks along the.
Processing XML Processing XML using XSLT Processing XML documents with Java (DOM) Next week -- Processing XML documents with Java (SAX)
XSL Concepts Lecture 7. XML Display Options What can XSL Transformations do? generation of constant text suppression of content moving text (e.g., exchanging.
XML DOM and SAX Parsers By Omar RABI. Introduction to parsers  The word parser comes from compilers  In a compiler, a parser is the module that reads.
XSL Transformations Lecture 8, 07/08/02. Templates The whole element is a template The match pattern determines where this template applies Result element(s)
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
XSL Transformations Transforming XML document into other (XML) documents 1XSL Transformations.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
.NET and XML (or XML in.NET) David Oguns Matt Harding.
XML.NET Concepts and Implementation Badar Gillani.
Svetlin Nakov Telerik Software Academy Manager Technical Training DOM Parser, Streaming Parser, XPath, LINQ to.
Introducing LINQ to XML Florin−Tudor Cristea, Microsoft Student Partner.
XML Fundementals XML vs.. HTML XML vs.. HTML XML Document (elements vs. attributes) XML Document (elements vs. attributes) XML and RDBMS XML and RDBMS.
XML C#.NET Software Development. eXtensible Markup Language Markup language that describes data Markup language that describes data Stores data as text.
1 Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COT 6930 Advanced Internet Programming Dr. Roy Levow Day 2.
1 XML Data Management 4. Domain Object Model Werner Nutt.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
1 Using XML in.NET Sorting through the choices. 2 XML and API’s XML is a series of W3C recommendations Focus on structure and behavior W3C says very little.
Presentation XML. NET SEMINAR By: Siddhant Ahuja (SID)
Copyright © Software Insight Querying and Transforming XML with.NET.
Representing Web Data: XML CSI 3140 WWW Structures, Techniques and Standards.
Extending System.Xml Ted Neward
17 Apr 2002 XML Programming - DOM Andy Clark. DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
DOM Robin Burke ECT 360. Outline XHTML in Schema JavaScript DOM (MSXML) Loading/Parsing Transforming parameter passing DOM operations extracting data.
Fall 2006 Florida Atlantic University Department of Computer Science & Engineering COP 4814 – Web Services Dr. Roy Levow Part 4 - XML.
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
The XML Document Object Model (DOM) Aug’10 – Dec ’10.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 34 - Case Study: Active Server Pages and XML Outline 34.1 Introduction 34.2 Setup and Message.
XML DOM Functionality in.NET DSK Chakravarthy
1 XSLT An Introduction. 2 XSLT XSLT (extensible Stylesheet Language:Transformations) is a language primarily designed for transforming the structure of.
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout.
1 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XML and the.NET framework Heavily inspired by: Support WebCast: Programming XML in the Microsoft.NET Framework.
XSLT Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML.
WORKING WITH XML IN THE.NET FRAMEWORK. Accessing an XML File Basic activities: open it, read it.NET Framework provides structured and unstructured mechanisms.
DAT 379 XML Today And Tomorrow Mark Fussell Lead Program Manager Microsoft Corporation.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Dr Alexiei Dingli XML Technologies SAX and DOM.
SDPLNotes 3.2: DOM1 3.2 Document Object Model (DOM) n How to provide uniform access to structured documents in diverse applications (parsers, browsers,
Introduction to the Document Object Model Eugenia Fernandez IUPUI.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
.NET and XML (or XML in.NET) Matt Harding David Oguns.
More XML XPATH, XSLT CS 431 – February 23, 2005 Carl Lagoze – Cornell University.
 XSL – Extensible Style Sheet Language  XSLT – XSL Transformations › Used to transform XML documents to other formats,like HTML or other XML documents.
Martin Kruliš by Martin Kruliš (v1.1)1.
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
1 XSLT XSLT (extensible stylesheet language – transforms ) is another language to process XML documents. Originally intended as a presentation language:
XML for .NET Session 1 Introduction to XML Introduction to XSLT
Displaying XML Data with XSLT
Concepts and Implementation
Introduction to the Document Object Model
DOM Robin Burke ECT 360.
XML in Web Technologies
More DOM 13-Nov-18.
DOM Document Object Model.
Processing XML.
More DOM 28-Nov-18.
More DOM.
XML DOM and CSS Instructors: Geoffrey Fox and Bryan Carpenter
04 | Data Acess Technologies
XML and its applications: 4. Processing XML using PHP
More XML XML schema, XPATH, XSLT
Presentation transcript:

C# The new language for Updated by Pavel Ježek © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (

2 XML in.NET.NET makes heavy use of XML –see ADO.NET, WSDL, UDDI, SOAP, … The base class library provides implementations for standards like: –XML, XSL, XPath,... Both XML processing models are supported: –DOM (Document Object Model) –serial access similar to SAX Namespaces –System.Xml –System.Xml.Xsl –System.Xml.XPath –System.Xml.Schema –System.Xml.Serialization

3 Processing XML Data XmlReader : Reading XML data XmlDocument, XmlNode : Object model of XML data (DOM) XmlWriter : Wrting XML data XPathNavigator : XPath selections XslTransform : Transformation of XML documents

4 XmlReader XmlReader for serial parsing Similar to SAX, but works with a pull mode Implementations are: –XmlTextReader : efficient, no immediate storage of elements –XmlValidatingReader : validates document against DTD or XSD –XmlNodeReader : reading from an XmlNode (DOM)

5 Class XmlReader public abstract class XmlReader { public abstract string Name { get; } public abstract string LocalName { get; } public abstract string Value { get; } public abstract XmlNodeType NodeType { get; } public abstract int AttributeCount { get; } public abstract int Depth { get; } public abstract bool Read(); public virtual string ReadElementString(string name ); public virtual XmlNodeType MoveToContent(); public virtual void Skip(); public abstract string GetAttribute(string name); public abstract void Close();... } Properties of current node -full name -local name -value -type -number of attributes -depth in document Reading of next node Get value of next element Move to next element Skipping the current element and its subs Getting the element‘s attributes Closing the reader

6 Example: XmlTextReader Output XmlTextReader r = null; try { r = new XmlTextReader("Addressbook.xml"); r.ReadStartElement("addressbook"); while (r.Read()) { if (r.NodeType == XmlNodeType.Element && r.Name == "person") { int Id = XmlConvert.ToInt32(r.GetAttribute("id")); r.Read();// Read next node string First = r.ReadElementString("firstname"); string Last = r.ReadElementString("lastname"); r.ReadElementString(" ");// Skip r.ReadEndElement();// Read Console.WriteLine("{0}: {1}, {2}.", Id, Last, First[0]); } } finally { if (r != null) r.Close(); } Wolfgang Beer Dietrich Birngruber Hanspeter Moessenboeck Albrecht Woess 1: Beer, W. 2: Birngruber, D. 3: Moessenboeck, H. 4: Woess, A.

7 DOM Construction of object structure in main memory +efficient manipulation of XML data –size limitations XML elements are represented by XmlElement objects, XML attributes by XmlAttribute objects, etc. (all inherited from XmlNode) XmlDocument object represents whole XML document Example: Loading an XML document: XmlDocument xDoc = new XmlDocument(); xDoc.Load("data.xml");

8 Example DOM Document xmladdressbook owner person firstname lastname id person firstname lastname id Wolfgang Beer Dietrich Birngruber

9 Class XmlNode (1) public abstract class XmlNode : ICloneable, IEnumerable, IXPathNavigable { public abstract string Name { get; } public abstract string LocalName { get; } public abstract XmlNodeType NodeType { get; } public virtual string Value { get; set; } public virtual XmlAttributeCollection Attributes { get; } public virtual XmlDocument OwnerDocument { get; } public virtual bool IsReadOnly { get; } public virtual bool HasChildNodes { get; } public virtual string Prefix { get; set; } public virtual XmlNodeList ChildNodes { get; } public virtual XmlNode FirstChild { get; } public virtual XmlNode LastChild { get; } public virtual XmlNode NextSibling { get; } public virtual XmlNode PreviousSibling { get; } public virtual XmlNode ParentNode { get; } public virtual XmlElement this[string name] { get; } public virtual XmlElement this[string localname, string ns] { get; } … Properties of node -full name -local name -type -value -attributes -… Accessing adjacent nodes -children -siblings -parent -named subnodes

10 Class XmlNode (2)... public virtual XmlNode AppendChild(XmlNode newChild); public virtual XmlNode PrependChild(XmlNode newChild); public virtual XmlNode InsertAfter(XmlNode newChild, XmlNode refChild); public virtual XmlNode InsertBefore(XmlNode newChild, XmlNode refChild); public virtual XmlNode RemoveChild(XmlNode oldChild); public virtual void RemoveAll(); public XPathNavigator CreateNavigator(); public XmlNodeList SelectNodes(string xpath); public XmlNode SelectSingleNode(string xpath); public abstract void WriteContentTo(XmlWriter w); public abstract void WriteTo(XmlWriter w);... } public enum XmlNodeType { Attribute, CDATA,Comment, Document, DocumentFragment, DocumentType, Element, EndElement, EndEntity, Entity, EntityReference, None, Notation, ProcessingInstruction, SignificantWhitespace, Text, Whitespace, XmlDeclaration } Adding and removing nodes Selection of nodes Writing

11 Class XmlDocument (1) public class XmlDocument : XmlNode { public XmlDocument(); public XmlElement DocumentElement { get; } public virtual XmlDocumentType DocumentType { get; } public virtual void Load(Stream in); public virtual void Load(string url); public virtual void LoadXml(string data); public virtual void Save(Stream out); public virtual void Save(string url); Root element Document type Loading the XML data Saving

12 Class XmlDocument (2) public event XmlNodeChangedEventHandler NodeChanged; public event XmlNodeChangedEventHandler NodeChanging; public event XmlNodeChangedEventHandler NodeInserted; public event XmlNodeChangedEventHandler NodeInserting; public event XmlNodeChangedEventHandler NodeRemoved; public event XmlNodeChangedEventHandler NodeRemoving; } public virtual XmlDeclaration CreateXmlDeclaration (string version, string encoding, string standalone); public XmlElement CreateElement(string name); public XmlElement CreateElement (string qualifiedName, string namespaceURI); public virtual XmlElement CreateElement (string prefix, string lName, string nsURI); public virtual XmlText CreateTextNode(string text); public virtual XmlComment CreateComment(string data); Creation of -declaration -elements -text nodes -comments Events for changes

13 Example: Creation of XML Document XmlDocument enables to built up XML documents Create document and add declaration Create root element Create and add Person element and subelements XmlDocument doc = new XmlDocument(); XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", null, null); doc.AppendChild(decl); XmlElement rootElem = doc.CreateElement("addressbook"); rootElem.SetAttribute("owner", "1"); doc.AppendChild(rootElem); XmlElement person = doc.CreateElement("person"); person.SetAttribute("id", "1"); XmlElement e = doc.CreateElement("firstname"); e.AppendChild(doc.CreateTextNode("Wolfgang")); person.AppendChild(e); e = doc.CreateElement("lastname");... Wolfgang Beer

14 XPath XPath is language for identification of elements in an XML document XPath expression (location path) selects a set of nodes A location path consists of location steps, which are separated by "/" //step/step/step/ Examples of location paths are: "*" selects all nodes "/addressbook/*" selects all elements under the addressbook elements "/addressbook/person[1]" returns the first person element of the addressbook elements "/addressbook/*/firstname“ returns the firstname elements under the addressbook Elements

15 XPathNavigator Class XPathNavigator provides navigation in document IXPathNavigable (implemented by XmlNode ) returns XPathNavigator public interface IXPathNavigable { XPathNavigator CreateNavigator(); } public abstract class XPathNavigator : ICloneable { public abstract string Name { get; } public abstract string Value { get; } public abstract bool HasAttributes { get; } public abstract bool HasChildren { get; } public virtual XPathNodeIterator Select(string xpath); public virtual XPathNodeIterator Select(XPathExpression expr); public virtual XPathExpression Compile(string xpath); public abstract bool MoveToNext(); public abstract bool MoveToFirstChild(); public abstract bool MoveToParent(); … } Properties of current node Selection of nodes by XPath expression Compilation of XPath expression Moving to adjacent nodes

16 Example: XPathNavigator Load XmlDocument and create XPathNavigator Select firstname elements, iterate over selected elements and put out name values For better run-time efficiency compile expression and use compiled expression XmlDocument doc = new XmlDocument(); doc.Load("addressbook.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathNodeIterator iterator = nav.Select("/addressbook/*/firstname"); while (iterator.MoveNext()) Console.WriteLine(iterator.Current.Value); XPathExpression expr = nav.Compile("/addressbook/person[firstname='Wolfgang']/ "); iterator = nav.Select(expr); while (iterator.MoveNext()) Console.WriteLine(iterator.Current.Value);

17 XML Transformation with XSL XSLT is XML language for transformations of XML documents XSL stylesheet is an XML document with a set of rules Rules (templates) define the transformation of XML elements XSLT is based on XPath; XPath expressions define the premises of the rules ( match ) In the rule body the generation of the transformation result is defined <xsl:template match=xpath-expression> construction of transformed elements

18 Example XSL Stylesheet XML Address Book

19 Example Transformation Original XML documentgenerated HTML document <META http-equiv="Content-Type" content="text/html; charset=utf-8"> XML-AddressBook Wolfgang Beer Dietrich Birngruber Wolfgang Beer Dietrich Birngruber

20 Class XslTransform Namespace System.Xml.Xsl provides support for XSLT Class XslTransform realizes XSL transformation public class XslTransform { public void Load(string url); public XslTransform(); public void Transform(string infile, string outfile, XmlResolver resolver);... // + overloaded methodds Load and Transform } Loading an XSLT stylesheet Transformation

21 Example: Transformation with XSL XslTransform xt = new XslTransform(); xt.Load("addressbook.xsl"); xt.Transform("addressbook.xml", "addressbook.html");