Download presentation
Presentation is loading. Please wait.
Published byBritney Austin Modified over 9 years ago
1
Presentation XML. NET SEMINAR By: Siddhant Ahuja (SID)
2
XML.NET Seminar by Sid Outline 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
XML.NET Seminar by Sid Outline 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
XML.NET Seminar by Sid Outline 8. XPath Queries 9. Summary and Q/A
5
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
6
XML.NET Seminar by Sid Introduction to XML Intro Mark up is: Adding Meaning to the Text Changing format of the Text Example: Sid
7
XML.NET Seminar by Sid Making an XML file Intro Windows XP SP2 This is the latest update pack provided by Microsoft. Has Firewall Protection Free ~200 MB for Windows XP Home Edition
8
XML.NET Seminar by Sid XML on the Web Intro
9
Windows XP SP2 This is the latest update pack provided by Microsoft. Has Firewall Protection Free ~200 MB for Windows XP Home Edition Root Element XML.NET Seminar by Sid XML Structure Intro Parent Element Child Element Attribute Content Declaration
10
XML.NET Seminar by Sid.NET Framework Base Class Library Common Language Specification Common Language Runtime ADO.NET: Data and XML VBC++C# Visual Studio.NET ASP.NET: Web Services and Web Forms JScript… Windows Forms
11
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
12
XML.NET Seminar by Sid 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
13
XML.NET Seminar by Sid XML.NET and Web Services XML and.NET Client Web Service Web Service Web Service Web Service Client XML XMLXML XML XML XML HTML
14
XML.NET Seminar by Sid 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
15
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
16
XML.NET Seminar by Sid DOM Parsing XML Parsing Models Beginning XML 40.00 XML Step by Step 50.00
17
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 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")
18
XML.NET Seminar by Sid 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)
19
XML.NET Seminar by Sid 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
20
XML.NET Seminar by Sid 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
21
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
22
XML.NET Seminar by Sid Using XmlTextWriter Class Dynamically generating XML in.NET XML Step by Step
23
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.Close() Using XmlTextWriter Class
24
XML.NET Seminar by Sid Using DOM 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
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
27
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.
28
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.
29
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
30
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.
31
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
32
XML.NET Seminar by Sid XslTransform ‘ 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
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
34
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
35
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
36
XML.NET Seminar by Sid Executing XPath Queries Sample XML document: The C Programming language XML Step by Step Sample XPath query: –Select all titles that begin with the word ‘XML’ XPath Query : //Title[starts-with(.,’XML’)]
37
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
38
XML.NET Seminar by Sid 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.