1 / 39

Presentation

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,

keenan
Download Presentation

Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Presentation XML . NET SEMINAR By: Siddhant Ahuja (SID)

  2. 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, • .NET Framework Parsing Models Forward only Pull Model Parsing DOM Parsing • Advantages and Limitations of each 4. Core XML.NET Namespaces

  3. Outline XML.NET Seminar by Sid • 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/A

  4. Outline XML.NET Seminar by Sid 8. XPath Queries 9. Summary and Q/A

  5. XML is Born XML.NET Seminar by Sid • Subset of SGML • XML is a mark up that describes structure • Acts as an integration protocol between applications • Industry standard developed by W3C XML and Microsoft. NET

  6. Introduction to XML XML.NET Seminar by Sid • Mark up is: • Adding Meaning to the Text • Changing format of the Text Example: <Presenter> Sid </Presenter> <Bold> Sid </Bold> Intro

  7. Making an XML file XML.NET Seminar by Sid • <?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> Intro

  8. XML on the Web XML.NET Seminar by Sid Intro

  9. XML Structure XML.NET Seminar by Sid Declaration • <?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 Element Parent Element Child Element Attribute Content Intro

  10. .NET Framework XML.NET Seminar by Sid VB C++ C# JScript … Visual Studio .NET Common Language Specification ASP .NET: Web Services and Web Forms WindowsForms ADO .NET: Data and XML Base Class Library Common Language Runtime

  11. XML and ADO.NET (unified architecture) Controls,Designers,Code-gen, etc. XSL/T, X-Path,Validation, etc. DataSet XmlData-Document Sync DataReader XmlReader DataAdapter XmlText- Reader XmlNode- Reader SqlData- Reader SqlData- Adapter OleDbData- Reader OleDbData- Adapter XML.NET Seminar by Sid

  12. XML and .NET XML.NET Seminar by Sid • 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 XML and .NET

  13. XML.NET and Web Services XML.NET Seminar by Sid Web Service Web Service XML XML XML XML Web Service Web Service Client XML HTML Client XML XML and .NET

  14. XML Parsing Models XML.NET Seminar by Sid • 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 XML Parsing Models

  15. DOM Model XML.NET Seminar by Sid • 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 XML Parsing Models

  16. DOM Parsing XML.NET Seminar by Sid <?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> XML Parsing Models

  17. DOM Parsing- Accessing and Modifying Element Data XML.NET Seminar by Sid Dim xmldoc As New XmlDocument() xmldoc.Load("c:\books.xml") Dim PriceNodes As XmlNodeList Dim 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 If Next xmldoc.Save("c:\books.xml") XML Parsing Models

  18. Core XML.NET Namespaces XML.NET Seminar by Sid • 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) Core XML.NET Namespaces

  19. Core XML.NET Namespaces XML.NET Seminar by Sid • 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 Core XML.NET Namespaces

  20. Dynamically generating XML in .NET XML.NET Seminar by Sid • Options available to programmatically generate XML: • Non-cached, forward-only streaming • Programming the DOM • Advantages and limitations of each method Dynamically generating XML in .NET

  21. Using XmlTextWriter Class XML.NET Seminar by Sid • 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 Dynamically generating XML in .NET

  22. Using XmlTextWriter Class XML.NET Seminar by Sid <?xml version="1.0"?> <!--Catalog fragment--> <!DOCTYPE Books SYSTEM "books.dtd"> <Books> <Book ISBN="0355605172"/> <Title>XML Step by Step</Title> </Book> </Books> Dynamically generating XML in .NET

  23. Using XmlTextWriter Class 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()

  24. Using DOM XML.NET Seminar by Sid Dim xmldoc As New XmlDocument() Dim xmldecl As XmlDeclaration Dim xmlComment As XmlComment Dim docType As XmlDocumentType Dim 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")

  25. Using DOM XML.NET Seminar by Sid 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

  26. Q A BrEaK

  27. Validation of XML XML.NET Seminar by Sid • 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. Validating XML in .NET

  28. Schemas in .NET XML.NET Seminar by Sid • 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. Validating XML in .NET

  29. Code Sample XML.NET Seminar by Sid 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

  30. XSLT Transformations in .NET XML.NET Seminar by Sid • 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. Applying Style-sheets to XML Documents

  31. XSLT in .NET XML.NET Seminar by Sid • 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 Applying Style-sheets to XML Documents

  32. XslTransform XML.NET Seminar by Sid ‘ Transforms the XML data in the input file and outputs the result to an output file Dim 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 XmlReader Dim 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 stream Dim xslt As XslTransform = New XslTransform() xslt.Load("books.xsl") Dim strm As New MemoryStream() xslt.Transform(New XPathDocument("books.xml"), Nothing, strm)

  33. Overview of XPath XML.NET Seminar by Sid • 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 XPath Queries

  34. XPath in .NET XML.NET Seminar by Sid • Related namespaces • System.Xml • System.Xml.XPath • Key .NET Framework classes • XmlDocument, XmlNodeList, and XmlNode • XPathDocument • XPathNavigator • XPathNodeIterator • XPathExpression XPath Queries

  35. Executing XPath Queries XML.NET Seminar by Sid 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

  36. Executing XPath Queries XML.NET Seminar by Sid • 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’)]

  37. Executing XPath Queries XML.NET Seminar by Sid 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

  38. Summary XML.NET Seminar by Sid • 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 Summary

  39. Q A EnD

More Related