Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 Part I Support WebCast: Programming XML in the Microsoft.NET Framework Part I XML related.NET Framework namespaces Introduction to XML parsing models in the.NET Framework DOM parsing in.NET the Framework Pull model XML parsing in the.NET Framework Generation of XML in the.NET Framework Validating XML documents XSLT transformations XPath queries

2 2 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Basic.NET Framework XML Namespaces System.Xml –Contains classes that offers standard support for xml –Supported W3C XML standards: XML 1.0 og XML namespaces XML schemas XPath XSLT DOM level 2 core SOAP 1.1 (used for fx. webservices)

3 3 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Basic.NET Framework XML Namespaces System.Xml.Xsl –Contains classes for XSLT transformations System.Xml.XPath –Contains classes for XPath queries System.Xml.Schema –Contains classes for standards-based support of W3C XML schemas System.Xml.Serialization –Contains classes used for serialization and deserialization of.NET Framework objects to / from XML

4 4 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XML Parsing Models XML parsing models : –DOM model (DOM = Document Object Model) –Forward only pull model parsing –Forward only push model parsing (SAX) Event based. The model is implemented in Java, but not in.NET Stores the tree as a data structure and traverse it e.g. with XPATH Traverse the XML- document and fires events for each node Traverse the XML- document with a cursor and fetch each node

5 5 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XML-based I/O XML-based I/O performed using a streaming interface suite –Models a stream of logical XML nodes (think Infoset) –Streaming done in pull-mode (read) and push-mode (write) –Built-in streaming adapters use System.IO.Stream –Abstract interfaces allow you to provide your own XML providers/consumers

6 6 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Working with a Stream of Nodes

7 7 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Non-Cached Parsing XmlReader and XmlWriter are abstract classes for reading and writing xml documents. The XmlReader is a “pull” model parser. MS says: The Pull model has several advances compared to the SAX “push” model: state management, selective processing, layering, etc. Sun & IBM can surely mention some advances in the SAX model Could be multithreading Concrete implementation: XmlTextReader - offers non-cached, forward-only, read-only access XmlTextWriter – offers a fast non-cached forward-only way of generating XML

8 8 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlReader XmlReader models reading a stream of nodes –XmlReader is an abstract class (somewhat interface-like) –Pull-model pushes complexity into provider implementation –Manages a forward/read-only cursor over a stream of nodes –Provides properties for inspecting current node –Nodes are processed in document order (depth-first traversal)

9 9 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Moving the Cursor Read is the only method that advances the cursor while (reader.Read()) { // process current node here } cursorcursor’ Read

10 10 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Read Helpers XmlReader provides several helpers that simplify Read –MoveToContent facilitates dealing with non-content nodes –Skip moves past the end of the current node –ReadStartElement simplifies validation code by skipping non-content nodes and checking current node before moving –ReadEndElement checks that current node is EndElement before moving –ReadElementString reads text within text-only elements and advances the cursor past EndElement

11 11 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Example private static void XmlReaderEx(){ XmlReader r = new XmlTextReader("author.xml"); string id, first, last, phone; r.ReadStartElement("author", "urn:authors"); r.ReadStartElement("name"); first = r.ReadElementString("first"); last = r.ReadElementString("last"); r.ReadEndElement(); //name phone = r.ReadElementString("phone"); r.ReadEndElement(); //a:author r.Close(); Console.WriteLine("Name: {0} {1}\nTelf: {2}", first, last, phone); Console.ReadLine(); } <x:author xmlns:x="urn:authors" id="333-33-3333"> Bob Smith 333-3333

12 12 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Reading Attribute Nodes XmlReader handles attributes differently –Attribute nodes are not part of sequential stream –Cursor must be on the element to process attributes –GetAttribute/Item retrieve value by name/index –MoveToAttribute moves to attribute by name/index –MoveToElement returns to owner element node –MoveToFirstAttribute/MoveToNextAttribute to iterate cursor attributes

13 13 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Example private static void MoveToContentEx(){ XmlReader r = new XmlTextReader("author.xml"); string id, first, last, phone; r.MoveToContent(); // should be on author id = r["id"]; r.ReadStartElement("author", "urn:authors"); r.ReadStartElement("name"); first = r.ReadElementString("first"); last = r.ReadElementString("last"); r.ReadEndElement(); //name phone = r.ReadElementString("phone"); r.ReadEndElement(); //a:author r.Close(); Console.WriteLine("Id: {3}\nName: {0} {1} \nTelf: {2}", first, last, phone, id); Console.ReadLine(); } <x:author xmlns:x="urn:authors" id="333-33-3333"> Bob Smith 333-3333

14 14 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlReader Implementations Many implementations of XmlReader are possible –XmlTextReader uses a TextReader for I/O over XML 1.0 –XmlNodeReader uses an XmlNode as its input source –XmlValidatingReader is a concrete class that provides XML Schema, XDR, and DTD validation while reading, along with additional type information (more on this later) –Custom readers can expose your own data as XML

15 15 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlReader Implementations

16 16 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlTextReader, summary Fast way of parsing XML data Assumes well-formedness Some properties can be modified before reading: WhitespaceHandling, XmlResolver, etc. Offers methods to skip content (more effiecient) The MoveToContent method moves directly to the content of an element, and skips fx. comments, attributtes and whitespaces The Skip method skips children of the actual node

17 17 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) The XmlTextReader class Able to read data from e.g. File, Stream and TextReader Key members: –Methods that begins with “MoveTo” are used for navigating to the actual attributte, element or node –Methods that begins with “Read” are used for: Reading the content of XML data, attribute values etc. Reading various format formatters (Hex, Base64, etc.) and returning decoded binary bytes –Properties: Encoding, Depth, LineNumber, ReadState, Value, etc.

18 18 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Generation of XML in the.NET Framework There is to ways in.Net to generate XML: –Non-cached, forward-only streaming (XmlTextWriter) –Creating the document by DOM

19 19 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlWriter XmlWriter models writing a stream of nodes –XmlWriter is an abstract class (somewhat interface-like) –Custom writers inevitable but not as common as readers –Push-mode interface balances complexity between writer implementation and user

20 20 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Writing Nodes XmlWriter provides several methods for writing nodes –Sequence of start/end method calls defines structure –Text nodes written via WriteString –Binary data written via WriteBinHex & WriteBase64 –WriteElementString helper writes text-only elements –Attribute nodes written via WriteStartAttribute, WriteEndAttribute or WriteAttributeString methods

21 21 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) public static void WriteAuthorDocument() { XmlTextWriter tw = new XmlTextWriter(Console.Out); tw.Formatting = Formatting.Indented; tw.WriteStartDocument(); tw.WriteStartElement("x", "author", "urn:authors"); tw.WriteAttributeString("id", id); tw.WriteStartElement("name"); tw.WriteElementString("first", first); tw.WriteElementString("last", last); tw.WriteEndElement(); // name tw.WriteElementString("phone", phone); tw.WriteEndElement(); // author tw.WriteEndDocument(); } Using XmlWriter

22 22 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlWriter Implementations

23 23 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlTextWriter, summary Implemented in System.Xml namespace Inherits from the abtract System.Xml.XmlWriter class Used for writing XML in a non-cached and forward-only way to fx. a file or a stream (may be a tcp-socket, directly to the console etc.)

24 24 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Commonly used properties and method on XmlTextWriter Properties Formatting (should output be indented) Indentation(How many char is an indent) QuoteChar(Use " or ' ) IndentChar(Which char is used for the indent, normally space) WriteState(Open or Close) Methods WriteXXX metoder – (fx.: WriteStartDocument, WriteStartElement, WriteAttributeString, etc.) Close

25 25 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Normal use of XmlTextWriter Create a XmlTextWriter object Choose formatting of output by setting formatting and indentation properties Execute the WriteXXX methods to generate the content of the XML document Execute the Close() method

26 26 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XML Traversal System.Xml provides two traversal APIs –The Document Object Model (DOM) –XPathNavigator cursor-based traversal –Both support XPath and manual traversal

27 27 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) DOM - XmlNode and XmlDocument DOM implemented as a hierarchy of concrete classes –XmlNode is at root of hierarchy –XmlDocument supports loading/serializing trees –XmlDocument is generic node factory –XmlNode-hierarchy is extensible

28 28 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XmlNode Hierarchy

29 29 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Loading/Serializing Documents XmlDocument loads and serializes documents –Load method loads from file, TextReader, or XmlReader –Save method saves to a file, TextWriter, or XmlWriter –Load/Save process customized via readers/writers –LoadXml loads the document from a string of XML 1.0 –InnerXml/OuterXml serialize node to string of XML 1.0

30 30 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Loading XmlDocument

31 31 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Serializing XmlDocument

32 32 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) static void Main(string[] args) { XmlDocument doc; doc = new XmlDocument(); doc.Load("author.xml");... // modify author here // serialize document to file on disk doc.Save("modified-author.xml"); // serialize to an XmlWriter (console) XmlTextWriter wr = new XmlTextWriter(Console.Out); wr.Formatting = Formatting.Indented; doc.Save(wr); // serialize to string Console.WriteLine(doc.InnerXml); } Loading and Saving a Document

33 33 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Navigating DOM Trees XmlNode provides properties for navigating DOM trees –Node inspection works like XmlReader –ChildNodes/FirstChild/LastChild access a node's children –NextSibling/PreviousSibling access a node's siblings –ParentNode accesses a node's parent/owner document –OwnerDocument accesses the root XmlDocument node

34 34 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) public void processAuthor1() { XmlDocument doc = new XmlDocument(); doc.Load("author.xml"); XmlNode first = doc.DocumentElement.FirstChild.FirstChild; XmlNode last = doc.DocumentElement.FirstChild.LastChild; Console.WriteLine("Hello {0} {1}", first.InnerText, last.InnerText); } Traversing a DOM Tree <x:author xmlns:x="urn:authors" id="333-33-3333"> Bob Smith 333-3333

35 35 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Simplifying Traversal with XPath XPath can greatly simplify traversal code –XmlNode provides two convenience methods for evaluating XPath expressions –SelectNodes identifies the set of nodes that match the expression (returns an XmlNodeList) –SelectSingleNode identifies first XmlNode that matches –Overloads exist to handle namespace support Use XmlNamespaceManager to hold namespace bindings

36 36 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XPath and SelectNodes public void ProcessAuthor2() { XmlDocument doc = new XmlDocument(); doc.Load("author.xml"); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("x", "urn:authors"); XmlNodeList names = doc.SelectNodes("/x:author/name/*", ns); Console.WriteLine("Hello {0} {1}", names[0].InnerText, names[1].InnerText); } <x:author xmlns:x="urn:authors" id="333-33-3333"> Bob Smith 333-3333

37 37 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Summary XmlReader is a streaming/pull-model interface for reading XmlWriter is a streaming/push-model interface for writing XML traversal can simplify (streaming) code DOM provides in-memory tree for navigation DOM tree loaded via XmlReader, serialized via XmlWriter DOM supports processing XPath queries View Code: XMLSamplesNew

38 38 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XPath in.NET Related namespaces System.Xml System.Xml.XPath Most important.NET Framework classes XmlDocument, XmlNodeList, and XmlNode XPathDocument XPathNavigator XPathNodeIterator XPathExpression

39 39 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Execute XPath queries with System.Xml DOM objects Common classes: XMLDocument, XMLNodeList, XMLNode Load XML dokumentet into an XMLDocument object Use the selectNodes/selectSingleNode method in the XmlDocument class to execute the XPath query Assign the returned nodelist or node to a XmlNodeList/XmlNode object Use a XmlNode object to iterate through the XmlNodeList and handle the result.

40 40 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) System.XML.XPath classes XPathDocument –Similar to the XmlDocument DOM object –Optimized for XSLT processing and XPath –Differs from the DOM object by Do not maintain node identity Do not validate the XML document XPathNavigator –Used for executing a XPath query on XPathDocument (Select method) –Used for compiling often used XPath query expressions (Compile method) –Created with the CreateNavigator() method in the XPathDocument class

41 41 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) System.XML.XPath classes (2) XPathNodeIterator –Offers a read-only cursor style interface for navigating in the result of a XPath query –Created by executing the Select( ) method on a XPathNavigator object XPathExpression –Used for storing a compiled instance of a often used XPath query expression –Created by calling the Compile(“ ”) method on a XPathNavigator object –Can be used as the parameter of the XPathNavigator’s Select method

42 42 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) System.XML.XPath classes (3) XPathDocument XPathNavigator XPathNodeIterator Load XML.CreateNavigator() Select(“ ”) XPathExpression.Compile(“ ”) Select(XPathExpression) Do While.MoveNext() ‘Process results Loop

43 43 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Eksempel: Navigator XPathDocument XPathDoc; XPathNavigator XPathNav; XPathExpression XPathExpr; XPathNodeIterator XPNodeIterator; XPathDoc = new XPathDocument("books.xml"); XPathNav = XPathDoc.CreateNavigator(); XPathExpr = XPathNav.Compile("//x:title[starts-with(.,'XML')]"); XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable()); ns.AddNamespace("x", "http://www.contoso.com/books"); XPathExpr.SetContext(ns); XPNodeIterator = XPathNav.Select(XPathExpr); if (XPNodeIterator.Count == 0) { Console.WriteLine("No matching nodes were identified for the specified XPath query"); } else { while (XPNodeIterator.MoveNext()) { Console.WriteLine(XPNodeIterator.Current.Value); } <book genre="autobiography" publicationdate="1981“ --- <book genre="Computer Science" publicationdate="2001“ ISBN="0415205173"> XML Step by Step Michael Young 50.00

44 44 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Execute XSLT transformations in.NET

45 45 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XSL Transformations XSL (Extensible Stylesheet Language) can be seen as 3 parts: XSLT – XSL transformations XPath – XML path language XSL-FO – XSL formatting objects XSLT is a language for transforming XML documents to text- based documents The transformation process involves 3 documents: Source XML document Stylesheet document Output document – XML, HTML, etc.

46 46 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XSLT in.NET Implemented in the System.Xml.Xsl namespace Supports the W3C XSLT 1.0 recommendation Important classes: XslTransform – Transforms XML data with a XSLT stylesheet XsltArgumentList – makes it possible to access parameters and extension objects from the stylesheet. XsltException – Returns information of the last exception that is thrown by the XSL transformation

47 47 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XSLT architecture in.NET

48 48 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XslTransform class XSLT stylesheetet shall include the namespace declaration "xmlns:xsl= http://www.w3.org/1999/XSL/Transform" Important methods: Load –Loads the XSLT stylesheet inclusive xsl:include and xsl:import references –Overloaded methods can load the stylesheet with XmlReader, XPathNavigator, or a URL Transform –Transforms XML data and returns the result –Is overloaded so it can handle different input and output formats as Stream, File, XMLReader, etc.

49 49 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Example: XslTransform private static void XslTransform1() private static void XslTransform1() { //Transforms the XML data in the input file and outputs the result to an output file //Transforms the XML data in the input file and outputs the result to an output file XslTransform xslt = new XslTransform(); XslTransform xslt = new XslTransform(); xslt.Load("books.xsl"); xslt.Load("books.xsl"); xslt.Transform("books.xml", "output.xml"); xslt.Transform("books.xml", "output.xml"); // Transforms the XML data in the XPathDocument and outputs the result to an XmlReader // Transforms the XML data in the XPathDocument and outputs the result to an XmlReader String URL = "http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/VB/"; String URL = "http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/VB/"; xslt = new XslTransform(); xslt = new XslTransform(); xslt.Load(URL + "books.xsl"); xslt.Load(URL + "books.xsl"); XPathDocument doc = new XPathDocument(URL + "books.xml"); XPathDocument doc = new XPathDocument(URL + "books.xml"); XmlReader reader = xslt.Transform(doc, null); XmlReader reader = xslt.Transform(doc, null); //Transforms the XML data in the input file and outputs the result to a stream //Transforms the XML data in the input file and outputs the result to a stream xslt = new XslTransform(); xslt = new XslTransform(); xslt.Load("books.xsl"); xslt.Load("books.xsl"); xslt.Transform(new XPathDocument("books.xml"), null, Console.Out); xslt.Transform(new XPathDocument("books.xml"), null, Console.Out); }

50 50 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) XsltArgumentList klassen Klassen bruges af Transform metoden Kan bruges til at tilføje parametre og objekter, der bliver associeret med et namespace Vigtigste metoder: AddX, GetX, og RemoveX (hvor X = Param eller ExtenstionObject) –Metoder til at tilføje, hente eller fjerne parametre eller extension objects –Parameter bør være en W3C XPath type; Ellers bliver den konverteret til Double eller String Clear –Fjerner alle parametre og extension objects

51 51 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Code Sample: XsltArgumentList // Create the XslCompiledTransform and load the stylesheet. // Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = new XslCompiledTransform(); XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("order.xsl"); xslt.Load("order.xsl"); // Create the XsltArgumentList. // Create the XsltArgumentList. XsltArgumentList xslArg = new XsltArgumentList(); XsltArgumentList xslArg = new XsltArgumentList(); // Create a parameter which represents the current date and time. // Create a parameter which represents the current date and time. DateTime d = DateTime.Now; DateTime d = DateTime.Now; xslArg.AddParam("date", "", d.ToString()); xslArg.AddParam("date", "", d.ToString()); // Transform the file. // Transform the file. xslt.Transform("order.xml", xslArg, XmlWriter.Create("output.xml")); xslt.Transform("order.xml", xslArg, XmlWriter.Create("output.xml")); xslt.Transform("order.xml", xslArg, Console.Out); xslt.Transform("order.xml", xslArg, Console.Out);

52 52 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Source files The Handmaid's Tale 19.95 Americana 16.95

53 53 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) General: –http://support.microsoft.com/WebCastshttp://support.microsoft.com/WebCasts –http://www2.msdnaa.net/browsehttp://www2.msdnaa.net/browse XML in.NET, MSDN Magazine, Skonnard –http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspxhttp://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx Writing XML Providers in.NET, MSDN Magazine, Skonnard –http://msdn.microsoft.com/msdnmag/issues/01/09/xml/default.aspxhttp://msdn.microsoft.com/msdnmag/issues/01/09/xml/default.aspx Links

54 54 XMLXML and.NETNOEA / PQC 2005 (rev. FEN 2007) Exercise Gennemgå og afprøv eksemplerne i kap.4.7 i Hanspetter Løs opgave 4.29 og 4.31 Solution: Tasks.zip


Download ppt "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."

Similar presentations


Ads by Google