1 xmlxml and.netnoea / pqc 2005 (rev. fen 2007) xml and the.net framework heavily inspired by:...
TRANSCRIPT
1 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
XML and the .NET framework
• Heavily inspired by: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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
<s:Envelope xmlns:s='...'> <s:Body> <m:Add xmlns:m='urn:math'> <x>33.3</x> <y>66.6</y> </m:Add> </s:Body></s:Envelope>
Document
Element: Envelope
Element: Add
Element: x
Text: 33.3
EndElement:x
XmlTextReader
XmlReader
XmlTextWriter
XmlWriter
Element: y
Element: Body
Text: 66.6
Working with a Stream of Nodes
7 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
Moving the Cursor
• Read is the only method that advances the cursor
while (reader.Read()){ // process current node here}
cursor cursor’
Read
10 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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();}
<?xml version="1.0" encoding="utf-8" ?><x:author xmlns:x="urn:authors" id="333-33-3333"> <name> <first>Bob</first> <last>Smith</last> </name> <phone>333-3333</phone></x:author>
12 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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();}
<?xml version="1.0" encoding="utf-8" ?><x:author xmlns:x="urn:authors" id="333-33-3333"> <name> <first>Bob</first> <last>Smith</last> </name> <phone>333-3333</phone></x:author>
14 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
XmlTextReader
XmlReader
XmlNodeReader
XmlReader
YourReader
XmlReader
YourApplication
or Data
XmlNode
XmlNode XmlNode
XmlNode
<s:Envelope xmlns:s='...'> <s:Body> <m:Add xmlns:m='urn:math'> <x>33.3</x> <y>66.6</y> </m:Add> </s:Body></s:Envelope>
Document Element: Envelope Element: Add Element: x Text: 33.3Element: Body ...
XmlReader Implementations
16 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
XmlTextWriter
XmlWriter
XmlNodeWriter
XmlWriter
YourWriter
XmlWriter
YourApplication
or Data
XmlNode
XmlNode XmlNode
XmlNode
<s:Envelope xmlns:s='...'> <s:Body> <m:AddResponse xmlns:m='urn:math'> <sum>99.9</sum> </m:Add> </s:Body></s:Envelope>
Document Element: Envelope Element: AddResponse Element: sumElement: Body Text: 99.9
XmlWriter Implementations
23 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
XmlWhitespace
XmlSignificantWhitespace
XmlNode
XmlCharacterData
XmlLinkedNode
XmlComment
XmlCDataSection
XmlText
XmlEntityReference
XmlProcessingInstruction
XmlDocumentType
XmlDeclaration
XmlElement
XmlAttribute
XmlDocumentFragment
XmlEntity
XmlNotation
XmlDocument
XmlNode Hierarchy
29 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
YourApplication
or Data
XmlTextReader
XmlReader
YourReader
XmlReader
XmlDocument
XmlElement
XmlElementXmlElement
XmlText XmlText
XmlElement
<s:Envelope xmlns:s='...'> <s:Body> <m:Add xmlns:m='urn:math'> <x>33.3</x> <y>66.6</y> </m:Add> </s:Body></s:Envelope> XmlElement
Loading XmlDocument
31 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
YourWriter
XmlWriter
XmlTextWriter
XmlWriter
XmlDocument
XmlElement
XmlElementXmlElement
XmlText XmlText
XmlElement
XmlElement
YourApplication
or Data
<s:Envelope xmlns:s='...'> <s:Body> <m:Add xmlns:m='urn:math'> <x>33.3</x> <y>66.6</y> </m:Add> </s:Body></s:Envelope>
Serializing XmlDocument
32 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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
<?xml version="1.0" encoding="utf-8" ?><x:author xmlns:x="urn:authors" id="333-33-3333"> <name> <first>Bob</first> <last>Smith</last> </name> <phone>333-3333</phone></x:author>
35 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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);}
<?xml version="1.0" encoding="utf-8" ?><x:author xmlns:x="urn:authors" id="333-33-3333"> <name> <first>Bob</first> <last>Smith</last> </name> <phone>333-3333</phone></x:author>
37 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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(<XPath query/expression>)
method on a XPathNavigator object• XPathExpression
– Used for storing a compiled instance of a often used XPath query expression
– Created by calling the Compile(“<XPath query>”) method on a XPathNavigator object
– Can be used as the parameter of the XPathNavigator’s Select method
42 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
System.XML.XPath classes (3)
XPathDocument
XPathNavigator
XPathNodeIterator
Load XML
.CreateNavigator()
Select(“<XPath Query>”)
XPathExpression
.Compile(“<XPath query>”)
Select(XPathExpression)
Do While .MoveNext() ‘Process resultsLoop
43 XML XML and .NET NOEA / 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); } }
<?xml version="1.0" encoding="utf-8" ?><bookstore xmlns="http://www.contoso.com/books"> <book genre="autobiography" publicationdate="1981“
--- </book> <book genre="Computer Science" publicationdate="2001“ ISBN="0415205173"> <title>XML Step by Step</title> <author> <name>Michael Young</name> </author> <price>50.00</price> </book></bookstore>
45 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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
48 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / 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 = XslTransform xslt = new new XslTransform();XslTransform(); xslt.Load(xslt.Load("books.xsl");"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 = String URL = "http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/VB/";"http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/VB/"; xslt = xslt = new new XslTransform();XslTransform(); xslt.Load(URL + xslt.Load(URL + "books.xsl");"books.xsl");
XPathDocument doc = XPathDocument doc = new new XPathDocument(URL + XPathDocument(URL + "books.xml");"books.xml"); XmlReader reader = xslt.Transform(doc, XmlReader reader = xslt.Transform(doc, null);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 = xslt = new new XslTransform();XslTransform(); xslt.Load(xslt.Load("books.xsl");"books.xsl");
xslt.Transform(xslt.Transform(new new XPathDocument(XPathDocument("books.xml"), "books.xml"), null, null, Console.Out);Console.Out); }}
50 XML XML and .NET NOEA / 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 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
Code Sample: XsltArgumentList
// Create the XslCompiledTransform and load the stylesheet.// Create the XslCompiledTransform and load the stylesheet. XslCompiledTransform xslt = XslCompiledTransform xslt = new new XslCompiledTransform();XslCompiledTransform(); xslt.Load(xslt.Load("order.xsl");"order.xsl");
// Create the XsltArgumentList.// Create the XsltArgumentList. XsltArgumentList xslArg = XsltArgumentList xslArg = new new XsltArgumentList();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(xslArg.AddParam("date", "", d.ToString());"date", "", d.ToString());
// Transform the file.// Transform the file. xslt.Transform(xslt.Transform("order.xml", xslArg, "order.xml", xslArg, XmlWriter.Create(XmlWriter.Create("output.xml"));"output.xml")); xslt.Transform("order.xml", xslArg, xslt.Transform("order.xml", xslArg, Console.Out);Console.Out);
52 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
Source files
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="date"/> <xsl:template match="/"> <order> <date> <xsl:value-of select="$date"/> </date> <total> <xsl:value-of select="sum(//price)"/> </total> </order> </xsl:template></xsl:stylesheet>
<?xml version="1.0" encoding="utf-8" ?><!--Represents a customer order--><order> <book ISBN='10-861003-324'> <title>The Handmaid's Tale</title> <price>19.95</price> </book> <cd ISBN='2-3631-4'> <title>Americana</title> <price>16.95</price> </cd></order>
53 XML XML and .NET NOEA / PQC 2005
(rev. FEN 2007)
• General:– http://support.microsoft.com/WebCasts– http://www2.msdnaa.net/browse
• XML in .NET, MSDN Magazine, Skonnard– http://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.aspx
Links